LeetCode—156. Binary Tree Upside Down

Binary Tree Upside Down思路:显然是递归去做的


GitHub地址:https://github.com/corpsepiges/leetcode

点此进入如果可以的话,请点一下star,谢谢。



/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode upsideDownBinaryTree(TreeNode root) {
        if (root==null) {
            return null;
        }else if (root.left!=null) {
            TreeNode ans=upsideDownBinaryTree(root.left);
            TreeNode test=ans;
            while (test.right!=null) {
                test=test.right;
            }
            if (root.right!=null) {
                test.left=new TreeNode(root.right.val);
            }
            test.right=new TreeNode(root.val);
            return ans;
        }else{
            return new TreeNode(root.val);
        }
    }
}


你可能感兴趣的:(leetcode)