void preorder_nonrec() //非遞迴
void inorder_nonrec()
void postorder_nonrec()
int size()
int leafsize()
int getklevelsize(int k)
int depth()
protected:
void _destory(binarytreenode*& root)
}binarytreenode*& _copybinarytree(binarytreenode*& root)
if (root->_rightchild)
return copyroot;
}void _createtree(binarytreenode*& root, t*& str) //建立節點
}void _preorder(binarytreenode*& root) //先序遍歷 跟,左,右
}void _inoreder(binarytreenode*& root) //中序遍歷 左,跟,右
}void _postorder(binarytreenode*& root) //後序遍歷 左,右,跟
}void _levelorder(binarytreenode*& root)
//佇列中的資料為1 2 5 3 4 6
while (!q.empty())
if (front->_rightchild != null)}}
//方法一
//int _size(binarytreenode*& root)
//// if (root->_leftchild == null && root->_rightchild == null) //葉子節點
//
// //左子樹節點的個數 + 右子數節點的個數 + 1(根節點)
// return 1 + _size(root->_leftchild) + _size(root->_rightchild);
//}//方法二
int _size(binarytreenode*& root, int& size)
//int _leafsize(binarytreenode*& root) //葉子節點的個數
//void _leafsize(binarytreenode*& root,int& size) //葉子節點的個數
int _getklevelsize(binarytreenode*& root, int k) //根節點是第一層
int _depth(binarytreenode*& root)
size_t leftdepth = _depth(root->_leftchild);
size_t rightdepth = _depth(root->_rightchild);
return
1 + (leftdepth > rightdepth ? leftdepth : rightdepth);
}void _preorder_nonrec(binarytreenode*& root)
while (!s.empty())
if (top->_leftchild)}}
void _inorder_nonrec(binarytreenode*& root)
if(!s.empty())}}
void _postorder_nonrec(binarytreenode*& root)
if (!s.empty())
else
//右子數還沒有訪問}}
}private:
binarytreenode* _root;
};main.cpp
#include"binarytree.h"
void test()
void test1()
int main()
二叉樹先序 中序 後序遍歷
題目 用遞迴和非遞迴方式,分別按照二叉樹先序 中序和後序列印所有的節點。我們約定 先序遍歷順序為根 左 右 中序遍歷順序為左 根 右 後序遍歷順序為左 右 根。遞迴實現 遞迴遍歷二叉樹 先序 public void preorderrecur node head system.out.println...
二叉樹先序遍歷 中序遍歷 後序遍歷
輸入二叉樹的先序遍歷序列和中序遍歷序列,輸出該二叉樹的後序遍歷序列。非建二叉樹版本 include includeusing namespace std string preord,inord void rebuild int preleft,int preright,int inleft,int ...
二叉樹先序遍歷 後序遍歷 中序遍歷
從根部 a 開始,然後開始遍歷左子樹,直接找到 b 檢視 b 有沒有左子樹,有 d,再檢視 d 有沒有子樹,沒有,d 已經是葉子,所以第二個是 d。倒回去,取中 b,第三個數是 b。檢視 b 有沒有右子樹,有 e 檢視 e 有沒有子樹,有 g 左 h 右 所有後面三個數是 egh 先查左子樹,存在繼...