1.  minimum-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.
求二叉树的最小深度;
 

解题思路

递归遍历二叉树的每一个节点:

(1)如果该节点为null,返回深度为0;

(2)如果节点不为空,返回左,右孩子中较小的那个孩子的深度+1;

注意:在(2)中容易出现的问题是,如果一个节点只有左孩子,没有右孩子,那么此时返回的应该是左孩子的深度,而不应该简单的认为返回两者的最小值;

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

2. binary-tree-postorder-traversal

题目描述

Given a binary tree, return the postorder traversal of its nodes' values. 后序遍历二叉树

For example:
Given binary tree{1,#,2,3},

   1
\
2
/
3

return[3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

解题思路:

后序遍历的顺序是:左->右->根,递归的方法很好写:

 public class Solution {
ArrayList<Integer> list = new ArrayList<Integer>();
public ArrayList<Integer> postorderTraversal(TreeNode root) {
if(root==null)
return list;
if(root.left!=null)
postorderTraversal(root.left);
if(root.right!=null)
postorderTraversal(root.right);
list.add(root.val);
return list;
}
}

非递归方法,使用栈来迭代:

要保证根结点在左孩子和右孩子访问之后才能访问,因此对于任一结点P,先将其入栈。
(1)如果P不存在左孩子和右孩子,则可以直接访问它;
(2)或者P存在孩子,但是其孩子都已被访问过了,则同样可以直接访问该结点
(3)若非上述两种情况,则将P的右孩子和左孩子依次入栈,这样就保证了
注意:每次取栈顶元素的时候,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问。
 public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
if(root==null)
return list;
Stack<TreeNode> S = new Stack<TreeNode>();
TreeNode preNode = null;
S.push(root);
while(!S.isEmpty()){
TreeNode curNode = S.peek();
if((curNode.left==null && curNode.right==null)||
(preNode != null && (preNode == curNode.left || preNode == curNode.right))){
list.add(curNode.val);
S.pop();
preNode = curNode;
}
else{
if(curNode.right!=null)
S.push(curNode.right);
if(curNode.left!=null)
S.push(curNode.left);
}
}
return list;
}
}

3. binary-tree-preorder-traversal

题目描述

Given a binary tree, return the preorder traversal of its nodes' values. 非递归前序遍历二叉树

For example:
Given binary tree{1,#,2,3},

   1
\
2
/

return[1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

解题思路:

利用栈的结构,先序遍历的顺序为 根左右,根先进栈,每次从栈中弹出一个节点访问,并把它的孩子(不为空)按先右后左的顺序入栈,直到栈为空;
 public class Solution {
public ArrayList<Integer> preorderTraversal(TreeNode root) {
Stack<TreeNode> S = new Stack<TreeNode>();
ArrayList<Integer> list = new ArrayList<Integer>();
if(root==null){
return list;
}
S.push(root);
while(!S.isEmpty()){
TreeNode tmp = S.pop();
list.add(tmp.val);
if(tmp.right!=null)
S.push(tmp.right);
if(tmp.left!=null)
S.push(tmp.left);
}
return list;
}
}

4. sum-root-to-leaf-numbers

题目描述

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path1->2->3which represents the number123.

Find the total sum of all root-to-leaf numbers.

For example,

    1
/ \
2 3

The root-to-leaf path1->2represents the number12.
The root-to-leaf path1->3represents the number13.

Return the sum = 12 + 13 =25.

从根节点到二叉树的叶子节点的每一条路径都有可以用一个数字表示,求这些数字的和;

解题思路:

用递归来做,主要就是考虑递归条件和结束条件。递归条件即是把当前的sum乘以10并且加上当前节点传入下一函数,进行递归,最终把左右子树的总和相加。结束条件的话就是如果一个节点是叶子,那么我们应该累加到结果总和中,如果节点到了空节点,则不是叶子节点,不需要加入到结果中,直接返回0即可。本质是一次先序遍历。

 public class Solution {
public int sumNumbers(TreeNode root) {
return Count(root, 0 );
}
static int Count(TreeNode root,int sum){
if(root==null)
return 0;
if(root.left==null&&root.right==null)
return sum*10+root.val;
return Count(root.left,sum*10+root.val)+Count(root.right,sum*10+root.val);
}
}

5. binary-tree-maximum-path-sum

题目描述

Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

找到二叉树中和最大的一条路径(任意节点为起点,不是根节点,不重复,不相交)

解题思路:

从根节点出发,递归地去找每个节点的左右子树的最大和的路径,返回当前节点的值+左右子树中和较大的那个;

注意,遍历每一条可能的路径时,都要更新最大和是否小于当前这条路径的和,是则更新;

 public class Solution {
public int sum = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
if(root==null)
return 0;
max(root);
return sum;
}
public int max(TreeNode root){
if(root==null)
return 0;
int left = Math.max(0, max(root.left));
int right = Math.max(0, max(root.right));
sum = Math.max(sum, left+right+root.val);
return Math.max(left, right)+root.val;
}
}

6. populating-next-right-pointers-in-each-node

题目描述

Given a binary tree

    struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.

Initially, all next pointers are set toNULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,
Given the following perfect binary tree,

         1
/ \
2 3
/ \ / \
4 5 6 7

After calling your function, the tree should look like:

         1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL

解题思路

由于是完全二叉树,所以每一个节点如果有左孩子,一定有右孩子,所以左孩子的next指向右孩子,
右孩子的next指向其父节点next的左孩子(如果父节点不为空),如果父节点的next为空,则他也指向空;
所以,如果我们知道每一层最左边的节点,那么就可以获得下一层的第一个节点以及下一层所有节点的父节点;
 public class Solution {
public void connect(TreeLinkNode root) {
if(root==null)
return;
root.next = null;
TreeLinkNode node = root, next = node;
while(node.left!=null){
next = node;
while(next!=null){
next.left.next = next.right;
if(next.next!=null)
next.right.next = next.next.left;
next = next.next;
}
node = node.left;
}
}
}

7. populating-next-right-pointers-in-each-node-ii

题目描述

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,
Given the following binary tree,

         1
/ \
2 3
/ \ \
4 5 7

After calling your function, the tree should look like:

         1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL

题目描述

与上题不同,给定的二叉树不一定是完全二叉树;
采用递归的方法,对于每一层,dummy指向每一层的第一个节点,对该层的每一个节点,处理它的孩子的next指向:
如果它有左孩子,那么prev的next就为当前节点的左孩子,并标记左孩子为prev;
如果它有右孩子,那么prev的next就为当前节点的右孩子,并标记右孩子为prev;
这样,无论是否是完全二叉树,都能实现next的正确指向。
 public class Solution {
public void connect(TreeLinkNode root) {
if(root == null)
return;
TreeLinkNode dummy = new TreeLinkNode(-1);
TreeLinkNode cur;
TreeLinkNode prev = dummy;
for(cur=root; cur !=null; cur = cur.next){
if(cur.left !=null)
{
prev.next = cur.left;
prev = prev.next;
}
if(cur.right !=null){
prev.next = cur.right;
prev = prev.next;
}
}
connect(dummy.next);
}
}

8. path-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.

For example:
Given the below binary tree andsum = 22,

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

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

解题思路

采用递归的思想:如果当前节点是叶子节点,且该节点的val等于sum,则返回true;

否则,递归第去判断该节点的左子树或者右子树的和是否满足sum-val。

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

9. path-sum-ii

题目描述

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree andsum = 22,

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

return

[
[5,4,11,2],
[5,8,4,5]
]
找到路径和为sum的所有路径

解题思路

判断方法和上一题一样,关键是路径的存储

 public class Solution {
public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
if(root==null)
return list;
ArrayList<Integer> ls = new ArrayList<Integer>();
find(list,ls,root,sum);
return list;
}
static void find(ArrayList<ArrayList<Integer>> list, ArrayList<Integer> ls, TreeNode root, int sum){
if(root==null)
return;
ls.add(root.val);
if(root.left==null && root.right==null && root.val==sum){
list.add(ls);
}
find(list,new ArrayList<Integer>(ls),root.left, sum-root.val);
find(list,new ArrayList<Integer>(ls),root.right, sum-root.val);
}
}

10. 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.

解题思路

递归地判断每颗子树是否为高度平衡二叉树(任一节点的左右子树高度差不超过1)

 public class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null)
return true;
if(Math.abs(getDepth(root.left)-getDepth(root.right))>1)
return false;
return (isBalanced(root.left) && isBalanced(root.right));
}
public int getDepth(TreeNode root){
if(root==null)
return 0;
return 1+Math.max(getDepth(root.left),getDepth(root.right));
}
}

11. binary-tree-level-order-traversal

题目描述

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree{3,9,20,#,#,15,7},

    3
/ \
9 20
/ \
15 7

return its level order traversal as:

[
[3],
[9,20],
[15,7]
]

解题思路

二叉树的层次遍历,用广度优先搜索,采用队列实现

 import java.util.*;
public class Solution {
public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(root == null){
return res;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
ArrayList<Integer> arr = new ArrayList<Integer>();
int size = queue.size();
TreeNode p = queue.peek();
for(int i=0;i<size;i++){
if(queue.peek().left!=null)
queue.offer(queue.peek().left);
if(queue.peek().right!=null)
queue.offer(queue.peek().right);
arr.add(queue.poll().val);
}
res.add(arr);
}
return res;
}
}

12. binary-tree-level-order-traversal-ii

解题思路

  与上题类似,只不过每层的数据按从底向上输出,仍然按照上题的方法遍历二叉树,但是每层的结果保存在一个栈中,最后按出栈顺序保存在list里;

 import java.util.*;
public class Solution {
public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
Stack<ArrayList<Integer>> stack = new Stack<ArrayList<Integer>>();
if(root==null)
return res;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
ArrayList<Integer> arr = new ArrayList<Integer>();
int size = queue.size();
for(int i=0;i<size;i++){
if(queue.peek().left!=null)
queue.offer(queue.peek().left);
if(queue.peek().right!=null)
queue.offer(queue.peek().right);
arr.add(queue.poll().val);
}
stack.push(arr);
}
while(!stack.isEmpty()){
res.add(stack.pop());
}
return res;
}
}

13. binary-tree-zigzag-level-order-traversal

解题思路

Z字形层次遍历二叉树

与上题类似,添加一个标志,如果是偶数层,反转该层的元素即可

 1 import java.util.*;
2 public class Solution {
3 public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
4 ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
5 if(root == null){
6 return res;
7 }
8 Queue<TreeNode> queue = new LinkedList<TreeNode>();
9 queue.offer(root);
10 boolean reserve = true;
11 while(!queue.isEmpty()){
12 reserve = !reserve;
13 ArrayList<Integer> arr = new ArrayList<Integer>();
14 int size = queue.size();
15 TreeNode p = queue.peek();
16 for(int i=0;i<size;i++){
17 if(queue.peek().left!=null)
18 queue.offer(queue.peek().left);
19 if(queue.peek().right!=null)
20 queue.offer(queue.peek().right);
21 arr.add(queue.poll().val);
22 }
23 if(reserve){
24 int len = arr.size();
25 int t = len/2;
26 for(int i=0;i<t;i++){
27 Collections.swap(arr,i,len-1-i);
28 }
29 }
30 res.add(arr);
31 }
32 return res;
33 }
34 }

14. maximum-depth-of-binary-tree

解题思路

  求二叉树的最大深度

  递归求左右子树的最大深度,每个节点的最大深度为左右子树的最大深度+1(自身)

 public class Solution {
public int maxDepth(TreeNode root) {
return root == null ? 0 : (1 + Math.max(maxDepth(root.left), maxDepth(root.right)));
}
}

15. symmetric-tree

给定一棵二叉树,判断其是否为对称的二叉树

解题思路

  递归判断左右子树是否完全相同即可

 public class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
return judge(root.left,root.right);
}
private static boolean judge(TreeNode a,TreeNode b){
if(b==null && a==null)
return true;
else if((a==null && b!=null) || (a!=null && b==null))
return false;
else
return (a.val==b.val && judge(a.left,b.right) && judge(a.right,b.left));
}
}

16. same-tree

判断两棵树是否完全相同

解题思路

  递归判断两个数的左子树是否完全相同且右子树完全相同

 public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q==null)
return true;
if( (p==null && q!=null) || (p!=null && q==null))
return false;
return (p.val==q.val && isSameTree(p.left,q.left) && isSameTree(p.right,q.right));
}
}

LeetCode 刷题笔记 (树)的更多相关文章

  1. LeetCode刷题总结-树篇(下)

    本文讲解有关树的习题中子树问题和新概念定义问题,也是有关树习题的最后一篇总结.前两篇请参考: LeetCode刷题总结-树篇(上) LeetCode刷题总结-树篇(中) 本文共收录9道题,7道中等题, ...

  2. LeetCode刷题总结-树篇(中)

    本篇接着<LeetCode刷题总结-树篇(上)>,讲解有关树的类型相关考点的习题,本期共收录17道题,1道简单题,10道中等题,6道困难题. 在LeetCode题库中,考察到的不同种类的树 ...

  3. LeetCode刷题总结-树篇(上)

          引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...

  4. C#LeetCode刷题-字典树

    字典树篇 # 题名 刷题 通过率 难度 208 实现 Trie (前缀树)   48.6% 中等 211 添加与搜索单词 - 数据结构设计   39.9% 中等 212 单词搜索 II   27.9% ...

  5. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  6. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  7. C#LeetCode刷题-线段树

    线段树篇 # 题名 刷题 通过率 难度 218 天际线问题   32.7% 困难 307 区域和检索 - 数组可修改   42.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 4 ...

  8. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  9. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

随机推荐

  1. jzyz集训 0612

    今天闵神找的题,题目质量还算不错,就是有些偏套路了. 第一题:定义一个排列是合法当且仅当这个排列中不存在大于2的循环节,询问有多少长度为n的排列是合法的且B排列是该排列的子序列. 恩...这题目其实不 ...

  2. .gitignore文件配置:keil工程文件类型【转】

    本文转载自:https://blog.csdn.net/u010160335/article/details/80043965 .gitignore源文件下载链接:git管理keil工程.gitign ...

  3. poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )

    Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13611   Accepted: 6138 ...

  4. python读取文件的几种方式

    http://www.cnblogs.com/nkwy2012/p/6023710.html

  5. 虚拟参考站(VRS)

    来源:https://www.sohu.com/a/149415053_391994 一.高精度定位 VRS是虚拟参考站(Virtual Reference Station)的简称.这项技术是CORS ...

  6. POJ 3620 Avoid The Lakes(dfs算法)

    题意:给出一个农田的图,n行m列,再给出k个被淹没的坐标( i , j ).求出其中相连的被淹没的农田的最大范围. 思路:dfs算法 代码: #include<iostream> #inc ...

  7. 集训Day7

    在做过的试题里ran的...发现之前做的题有些已经生疏了 bzoj3626 LCA 一棵树,每次询问在$[l,r]$区间内的每个节点$i$与$z$的最近公共祖先的深度之和 假的LCA 有一个很平凡的想 ...

  8. HihoCoder1656 : 前缀后缀查询([Offer收割]编程练习赛39)(字典树+小技巧)

    描述 给定一个包含N个单词的字典:{W1, W2, W3, ... WN},其中第i个单词Wi有具有一个权值Vi. 现在小Hi要进行M次查询,每次查询包含一个前缀字符串Pi和一个后缀字符串Si.他希望 ...

  9. Codeforces 755B. PolandBall and Game 贪心

    题目大意: 有两个人轮流说单词,已经说过的单词不能再说.给出两人掌握的不同的单词,两人可能掌握相同的单词,但是这个单词也只能说一边.问在两人都是最优策略下先手是否必胜. 题解: 我们发现最优策略一定是 ...

  10. css 跳转电脑分辨率

    因为我们经常在项目中要适配各种屏幕,为了方便前端的开发和测试.我们可以直接把电脑的分辨率调整到需要适配的最小的分辨率,其实还有一种更直接粗暴的方法.直接按F12打开控制台,在收拉浏览器就能看到目前的分 ...