码迷,mamicode.com
首页 > 其他好文 > 详细

156. Binary Tree Upside Down

时间:2018-12-09 16:39:24      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:void   col   oid   else   bsp   roo   span   ide   rev   

 

 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

标签:void   col   oid   else   bsp   roo   span   ide   rev   

原文地址:https://www.cnblogs.com/goPanama/p/10090807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!