二叉树遍历(层次)递归法

	输入:	{ 1,2 }
	返回值:[[1], [2]]


	class Solution {
	public:
		/**
		 *
		 * @param root TreeNode类
		 * @return int整型vector<vector<>>
		 */
		vector<vector<int> > levelOrder(TreeNode* root) {
			// write code here
			vector<vector<int>> a;
			cengorder(root, 0, a);
			return res;
		}
		void cengorder(TreeNode* root, int depth, vector<vector<int>> &a)  //与前序、中序、后序比较多了参数depth
		{
			if (!root)
				return;
			if (a.size() == depth)
				a.push_back(vector<int>());

			a[depth].push_back(root->val);
			cengorder(root->left, depth + 1, a);
			cengorder(root->right, depth + 1, a);
		}
	};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉链表表示二叉树的节点结构体如下: ```c++ struct BinaryTreeNode { int val; BinaryTreeNode* left; BinaryTreeNode* right; BinaryTreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; ``` 其中,val 表示节点的值,left 和 right 分别表示左子树和右子树的指针,如果左子树或右子树为空,则指针为 nullptr。 下面分别介绍二叉树递归遍历和非递归遍历。 ## 递归遍历 ### 前序遍历 前序遍历的顺序是:先遍历根节点,再遍历左子树,最后遍历右子树。递归如下: ```c++ void preorderTraversal(BinaryTreeNode* root) { if (root == nullptr) { return; } cout << root->val << " "; // 访问根节点 preorderTraversal(root->left); // 遍历左子树 preorderTraversal(root->right); // 遍历右子树 } ``` ### 中序遍历 中序遍历的顺序是:先遍历左子树,再遍历根节点,最后遍历右子树。递归如下: ```c++ void inorderTraversal(BinaryTreeNode* root) { if (root == nullptr) { return; } inorderTraversal(root->left); // 遍历左子树 cout << root->val << " "; // 访问根节点 inorderTraversal(root->right); // 遍历右子树 } ``` ### 后序遍历 后序遍历的顺序是:先遍历左子树,再遍历右子树,最后遍历根节点。递归如下: ```c++ void postorderTraversal(BinaryTreeNode* root) { if (root == nullptr) { return; } postorderTraversal(root->left); // 遍历左子树 postorderTraversal(root->right); // 遍历右子树 cout << root->val << " "; // 访问根节点 } ``` ## 非递归遍历递归遍历需要借助栈来实现。下面分别介绍前序、中序和后序遍历的非递归。 ### 前序遍历 前序遍历的非递归如下: ```c++ void preorderTraversal(BinaryTreeNode* root) { if (root == nullptr) { return; } stack<BinaryTreeNode*> st; st.push(root); while (!st.empty()) { BinaryTreeNode* node = st.top(); st.pop(); cout << node->val << " "; // 访问根节点 if (node->right != nullptr) { st.push(node->right); // 先将右子树入栈 } if (node->left != nullptr) { st.push(node->left); // 再将左子树入栈 } } } ``` ### 中序遍历 中序遍历的非递归如下: ```c++ void inorderTraversal(BinaryTreeNode* root) { if (root == nullptr) { return; } stack<BinaryTreeNode*> st; BinaryTreeNode* node = root; while (node != nullptr || !st.empty()) { while (node != nullptr) { st.push(node); node = node->left; // 一直向左走,直到左子树为空 } node = st.top(); st.pop(); cout << node->val << " "; // 访问根节点 node = node->right; // 遍历右子树 } } ``` ### 后序遍历 后序遍历的非递归比较复杂,需要用到两个栈来实现。具体实现如下: ```c++ void postorderTraversal(BinaryTreeNode* root) { if (root == nullptr) { return; } stack<BinaryTreeNode*> st1, st2; st1.push(root); while (!st1.empty()) { BinaryTreeNode* node = st1.top(); st1.pop(); st2.push(node); // 将访问过的节点压入 st2 中 if (node->left != nullptr) { st1.push(node->left); // 先将左子树入栈 } if (node->right != nullptr) { st1.push(node->right); // 再将右子树入栈 } } while (!st2.empty()) { BinaryTreeNode* node = st2.top(); st2.pop(); cout << node->val << " "; // 访问根节点 } } ``` 以上就是二叉树递归遍历和非递归遍历的实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值