• 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;
        }
    }
  • 相关阅读:
    Asp中返回到前一页面
    vs2008 简单ajax 功能的实现。
    Arcgis Server的唯一值渲染
    ArcGIS Server中缓冲区分析的实现(点)
    用两个Calendar控件来控制数据库记录的读入
    把十进制转化为二进制的一种方法
    Segmentation fault (core dumped)
    libc glibc glib 的关系
    使用异或加密数据
    宏定义一些内容
  • 原文地址:https://www.cnblogs.com/timoBlog/p/4639436.html
Copyright © 2020-2023  润新知