morris 树的中序遍历(O(1)空间 O(n)时间)

#include <iostream>


using namespace std;

struct TreeNode
{
	int val;
	TreeNode * left;
	TreeNode * right;
	TreeNode(int v):val(v),left(NULL),right(NULL){}
};


void InorderTraversal(TreeNode * root)
{
	if(root == NULL)
		return;
	TreeNode * pre = NULL;
	TreeNode * current = root;
	while(current != NULL)
	{
		if(current -> left == NULL)
		{
			cout << current->val << " ";
			current = current->right;
			continue;
		}
		pre = current->left;
		while(pre->right != NULL && pre->right != current)
			pre = pre->right;
		if(pre->right == NULL)
		{
			pre->right = current;
			current = current->left;
		}
		else
		{
			pre->right = NULL;
			cout << current->val << " ";
			current = current->right;
		}
	}
}

int main()
{
	TreeNode a(0), b(1), c(2),d(3),e(4);
	a.left = &b;
	a.right = &c;
	b.left = &d;
	b.right = &e;

	InorderTraversal(&a);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 编写非递归遍历算法 对于二叉树的遍历,常用的非递归方法有使用栈和Morris遍历。这里我们使用栈实现非递归遍历。 先序遍历: ```python def preorder(root): stack = [root] res = [] while stack: node = stack.pop() if node: res.append(node.val) stack.append(node.right) stack.append(node.left) return res ``` 中序遍历: ```python def inorder(root): stack = [] node = root res = [] while stack or node: while node: stack.append(node) node = node.left node = stack.pop() res.append(node.val) node = node.right return res ``` 后序遍历: ```python def postorder(root): stack = [(root, False)] res = [] while stack: node, visited = stack.pop() if node: if visited: res.append(node.val) else: stack.append((node, True)) stack.append((node.right, False)) stack.append((node.left, False)) return res ``` 2. 实现给定一棵二叉树的先序遍历序列和中序遍历序列,创建这棵。 我们可以使用递归的方式进行构建。先序遍历的第一个节点必定是根节点,然后在中序遍历中找到这个节点,左边的是左子,右边的是右子。 代码实现如下: ```python def buildTree(preorder, inorder): if not preorder or not inorder: return None root_val = preorder[0] root = TreeNode(root_val) idx = inorder.index(root_val) root.left = buildTree(preorder[1:idx+1], inorder[:idx]) root.right = buildTree(preorder[idx+1:], inorder[idx+1:]) return root ``` 3. 输出二叉树的后序遍历结点序列 后序遍历的顺序是左右根,可以使用递归的方式实现: ```python def postorderTraversal(root): if not root: return [] return postorderTraversal(root.left) + postorderTraversal(root.right) + [root.val] ``` 也可以使用非递归的方式实现,类似于非递归的先序遍历: ```python def postorderTraversal(root): if not root: return [] stack = [(root, False)] res = [] while stack: node, visited = stack.pop() if node: if visited: res.append(node.val) else: stack.append((node, True)) stack.append((node.right, False)) stack.append((node.left, False)) return res ``` 4. 输出二叉树的叶子结点 叶子结点是指没有左右子的节点,在遍历的过程中,遇到叶子结点就把它加到结果中即可。可以使用递归的方式实现。 ```python def getLeaves(root): if not root: return [] if not root.left and not root.right: return [root.val] return getLeaves(root.left) + getLeaves(root.right) ``` 也可以使用非递归的方式实现,使用栈来记录遍历的节点: ```python def getLeaves(root): if not root: return [] stack = [root] res = [] while stack: node = stack.pop() if node: if not node.left and not node.right: res.append(node.val) else: stack.append(node.right) stack.append(node.left) return res ``` 5. 统计二叉树的结点个数 二叉树的结点个数包括根节点、左子的结点个数和右子的结点个数。可以使用递归的方式实现。 ```python def countNodes(root): if not root: return 0 return countNodes(root.left) + countNodes(root.right) + 1 ``` 也可以使用非递归的方式实现,使用栈来记录遍历的节点: ```python def countNodes(root): if not root: return 0 stack = [root] count = 0 while stack: node = stack.pop() if node: count += 1 stack.append(node.left) stack.append(node.right) return count ``` 6. 求二叉树的深度 二叉树的深度等于左右子深度的最大值加1。可以使用递归的方式实现。 ```python def maxDepth(root): if not root: return 0 return max(maxDepth(root.left), maxDepth(root.right)) + 1 ``` 也可以使用非递归的方式实现,使用队列来记录遍历的节点,并记录每个节点的深度: ```python def maxDepth(root): if not root: return 0 queue = [(root, 1)] depth = 0 while queue: node, cur_depth = queue.pop(0) if node: depth = max(depth, cur_depth) queue.append((node.left, cur_depth+1)) queue.append((node.right, cur_depth+1)) return depth ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值