156. Binary Tree Upside Down

Posted gopanama

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了156. Binary Tree Upside Down相关的知识,希望对你有一定的参考价值。

 

 1 class Solution {
 2     TreeNode newRoot;
 3     TreeNode prev;
 4     public TreeNode upsideDownBinaryTree(TreeNode root) {
 5         if(root == null) return root;
 6         helper(root);
 7         return newRoot;
 8     }
 9     
10     public void helper(TreeNode root){
11         if(root.left != null){
12             helper(root.left);
13         }else{
14             newRoot = new TreeNode(root.val);
15             prev = newRoot;
16             return;
17         }
18         if(root.right != null){
19             prev.left = new TreeNode(root.right.val);
20         } 
21         prev.right = new TreeNode(root.val);
22         prev = prev.right;
23     }
24 }

 

以上是关于156. Binary Tree Upside Down的主要内容,如果未能解决你的问题,请参考以下文章

156. Binary Tree Upside Down

156. Binary Tree Upside Down

java 156. Binary Tree Upside Down.java

java 156. Binary Tree Upside Down.java

java 156. Binary Tree Upside Down.java

java 156. Binary Tree Upside Down.java