zoukankan      html  css  js  c++  java
  • [leetcode] Binary Tree Upside Down

    思路:递归实现,注意翻转的规律,以左子树作为根,找到左子树最右边的节点作为原有right子树和根的父节点。

    class Solution {
        public TreeNode upsideDownBinaryTree(TreeNode root) {
            if (root ==null || root.left ==null && root.right ==null)
                return root;
            TreeNode left = upsideDownBinaryTree(root.left);
            TreeNode right = upsideDownBinaryTree(root.right);
            TreeNode leftRightest = left;
            while(leftRightest.right != null){
                leftRightest = leftRightest.right;
            }
            leftRightest.left = right;
            leftRightest.right = root;
            root.left = null;
            root.right = null;
            
            return left;
        }
    }
  • 相关阅读:
    python小程序之购物系统
    列表,元祖,字典的使用
    几个python小程序
    default
    RTTI
    man
    养喜神去杀机
    IDEA+Maven+Git
    入门
    CheckStyle简介
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/7476353.html
Copyright © 2011-2022 走看看