本文主要是介绍leetcode114~Flatten Binary Tree to Linked List,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1/ \2 5/ \ \3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
public class FlattenTree {//使用栈实现public void flatten(TreeNode root) {
// if(root==null) return;Stack<TreeNode> stack = new Stack<TreeNode>();TreeNode node = root;while(node!=null || !stack.isEmpty()) {if(node.right!=null) {stack.push(node.right);}if(node.left!=null) {node.right=node.left;node.left=null;} else if(!stack.isEmpty()){TreeNode tmp = stack.pop();node.right=tmp;}node = node.right;}}//前序遍历二叉树,遇到左子节点时,将其变为右子节点,并将左置空//因此需要一个全局变量存储之前的右子节点private TreeNode lastNode = null;public void flatten2(TreeNode root) {if(root==null) return;if(lastNode !=null) {lastNode.left = null;lastNode.right = root;}lastNode = root;TreeNode right = root.right;flatten2(root.left);flatten2(right);}
}
这篇关于leetcode114~Flatten Binary Tree to Linked List的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!