zoukankan      html  css  js  c++  java
  • Leetcode & CTCI ---Day 2

    Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced.

    For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public boolean isBalanced(TreeNode root) {
            if (root == null)
                return true;
            if (getHeight(root) != -1)
                return true;
            return false;
        }
        
        public int getHeight(TreeNode root){
            if (root == null)
                return 0;
            int left_height = getHeight(root.left);
            int right_height = getHeight(root.right);
            if (left_height == -1 || right_height == -1)
                return -1;
            if (Math.abs(left_height - right_height) > 1)
                return -1;
            return Math.max(left_height, right_height) + 1;
        }
    }

    Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

    For example, this binary tree is symmetric:

        1
       / 
      2   2
     /  / 
    3  4 4  3
    

     

    But the following is not:

        1
       / 
      2   2
          
       3    3
    

     

    Note:
    Bonus points if you could solve it both recursively and iteratively.

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

    I did not figure how to do this fisrtly. Then I read some solution and figure it as recursive solution. I did not think about iterative solution this time. 

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public boolean isSymmetric(TreeNode root) {
            if (root == null)
                return true;
            return isSymmetric(root.left, root.right);
        }
        
        public boolean isSymmetric(TreeNode left, TreeNode right){
            if (left == null && right == null)
                return true;
            else if (left == null || right == null)
                return false;
            else if (left.val != right.val) return false;
            else if (!isSymmetric(left.left, right.right)) return false;
            else if (!isSymmetric(left.right, right.left)) return false;
            return true;
        }
    }
  • 相关阅读:
    A05. openstack架构实战-nova服务控制节点安装
    A04. openstack架构实战-glance服务安装
    A03. openstack架构实战-keystone安装
    SSH暴力破解的解读与防御
    tcpdump抓包命令使用
    Superspeed.sh 一键测试服务器到中国上传/下载速度
    Windows ping TCP端口工具之tcping
    实验验证centos 7 系统不会缓存dns信息
    Linux下的TCP测试工具——TCPING安装简明教程
    Debian 9.12 live系统密码重置
  • 原文地址:https://www.cnblogs.com/timoBlog/p/4639436.html
Copyright © 2011-2022 走看看