leetcode 110,111,112,118,119

110Balanced 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.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
   / \
  9  20
    /  \
   15   7

Return true.

方法一    Runtime: 2 ms

 /**
   *求每个节点高度
   *如果相等则递归向下计算
   *
   */
    public boolean isBalanced(TreeNode root) {
        if(root==null) return true;       
        int left = getdepth(root.left);
        int right = getdepth(root.right);
        if(Math.abs(left-right)>1)//如果高度差>1不平衡
        {
            return false;
        }
        return isBalanced(root.left)&&isBalanced(root.right);//继续判断
    }
    public int getdepth(TreeNode node)//求节点高度
    {
        if(node==null) return 0;
        int left = getdepth(node.left);
        int right = getdepth(node.right);
        return left>right ? left +1:right+1;
    }

方法2  Runtime: 2 ms 需遍历树

class Solution {
    public  boolean flag =true;
    public boolean isBalanced(TreeNode root) {
        getdepth(root);
        return flag;
    }
    public int getdepth(TreeNode node)
    {
        if(node==null) return 0;
        int left = getdepth(node.left);
        int right = getdepth(node.right);
        if(left-right>1||left-right<-1)
        {
            flag = false;
        }
        return left>right ? left +1:right+1;
    }
}
方法3  在方法2的基础上高改进不必遍历树 什么时候判断不平衡社么时候结束
class Solution {
    
    public boolean isBalanced(TreeNode root) {
        return getdepth(root)!=-1?true:false; 
       
    }
    public int getdepth(TreeNode node)
    {
        if(node==null) return 0;
        int left = getdepth(node.left);
        if(left == -1) return -1;  //加入判断 减少递归
        int right = getdepth(node.right);
        if(right == -1) return -1;
        if(left-right>1||left-right<-1)
        {
           return -1;
        }
        return left>right ? left +1:right+1;
    }
}

111Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its minimum depth = 2.


求根到叶子节点的最小高度

方法一   按层遍历树 如果某层有叶子节点则返回高度

class Solution {
    public int minDepth(TreeNode root) {
        if(root==null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int count=1;
        while(!queue.isEmpty())
        {
            int size = queue.size();
            while(size-->0)
            {
                TreeNode p = queue.poll();
                if(p.left==null&&p.right==null) return count;//判断叶子节点如果是叶子则返回结果
                if(p.left!=null) queue.offer(p.left);
                if(p.right!=null) queue.offer(p.right);   
            }
            count++;
        }
        return count;
    }
}

方法2

class Solution {
    public int minDepth(TreeNode root) {
      if(root==null) return 0;
      int left = minDepth(root.left);
      int right = minDepth(root.right);
      // left == 0   right ==0  高=1
     //  left != 0   right ==0  高=left +0 + 1
    //   left == 0   right !=0  高=0 +right +1
   //    left != 0   right !=0  高=Math.min(left,right)+1;
      return (left==0||right==0)? left+right+1 : Math.min(left,right)+1; 
        
    }
}

112Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

      5
     / \
    4   8
   /   / \
  11  13  4
 /  \      \
7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

判断到叶子节点有没有一条路径和为 sum

递归版

class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root==null) return false;
        int target = sum -root.val;
        if(root.left==null&&root.right==null) return target==0;
        return hasPathSum(root.left, target)||hasPathSum(root.right, target);
    }
}

非递归

class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
      if(root==null) return false;
        Stack<TreeNode> nodestack = new Stack<>();//遍历树
        nodestack.push(root);
        Stack<Integer> sumstack = new Stack<>();//遍历树的时候记录 根到该节点的和
        sumstack.push(root.val);
        int val=0;
        while(!nodestack.isEmpty())
        {
            TreeNode temp = nodestack.pop();
            val=sumstack.pop();
            if(temp.left==null&&temp.right==null)//如果为叶子则判断是否为sum
            {
                if(val==sum) return true;   
            }
            if(temp.left!=null) 
            {
                nodestack.push(temp.left);
                sumstack.push(temp.left.val+val);
            }
            if(temp.right!=null)
            {
                nodestack.push(temp.right);
                sumstack.push(temp.right.val+val);
            }
        }
        return false;
    }
}

118Pascal's Triangle


In Pascal's triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

杨辉三角

class Solution {
    public List<List<Integer>> generate(int numRows) {
      
            List<List<Integer>> res = new ArrayList<>();
            if(numRows==0) return res;
            ArrayList<Integer> row = new ArrayList<>();
            row.add(1);
            res.add(row);
	        List<Integer> temp;
	        for(int i=1;i<numRows;i++)//从第二行开始
	        {
	        	temp=res.get(i-1);//上一行
	        	List<Integer> add = new ArrayList<>();
                add.add(1);
	        	for(int j=1;j<temp.size();j++)
	        	{
	        		add.add(temp.get(j-1)+temp.get(j));	//当前行与上一行对应元素的关系
	        	}
	        	add.add(1);
                res.add(add);
	        }
	        return res;
        
    }
}

119Pascal's Triangle II


Example:

Input: 3
Output: [1,3,3,1]


获得rowIndex行的杨辉三角结果

方法一 在上题的基础上修改

 public List<Integer> getRow(int rowIndex) {
	       List<List<Integer>> res = new ArrayList<>();
         
            ArrayList<Integer> row = new ArrayList<>();
            row.add(1);
           
            res.add(row);
          if(rowIndex==0) return res.get(0);
	        List<Integer> temp;
	        for(int i=1;i<=rowIndex;i++)//从第二行开始
	        {
	        	temp=res.get(i-1);//上一行
	        	List<Integer> add = new ArrayList<>();
                add.add(1);
	        	for(int j=1;j<temp.size();j++)
	        	{
	        		add.add(temp.get(j-1)+temp.get(j));	//当前行与上一行对应元素的关系
	        	}
	        	add.add(1);
                res.add(add);
	        }
	        return res.get(rowIndex);
	    }

方法2

class Solution {
     
     public List<Integer> getRow(int rowIndex) {
	        List<Integer> row = new ArrayList<>();
          row.add(1);
          for(int i=1;i<=rowIndex;i++)
          {
              
              for(int j=0;j<row.size()-1;j++)
              {
                  row.set(j,row.get(j)+row.get(j+1));//新行1中间的数等于老行对应索引位置的数与后一个数的和 
              }
             
              row.add(0,1);
              
          }
           return row;
	}
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值