Path Sum II

思路:回溯  

public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if(root == null) return list;
List<Integer> inList = new ArrayList<Integer>();
getPath(root,sum,list,inList);
return list;
} public void getPath(TreeNode root,int sum,List<List<Integer>> list,List<Integer> inList){
if(root == null) return;
inList.add(root.val);
if(root.left == null && root.right == null && root.val == sum){
list.add(new ArrayList<Integer>(inList));
} getPath(root.left,sum - root.val,list,inList);
getPath(root.right,sum - root.val,list,inList);
inList.remove(inList.size() - 1);
return;
}

Path Sum III

思路:从头结点开始计算以头结点开始的路径条数,然后递归计算左节点和右节点  

public int pathSum(TreeNode root, int sum) {
if(root == null) return 0;
return dfs(root,sum) + pathSum(root.left,sum) + pathSum(root.right,sum);
} public int dfs(TreeNode root,int sum){
int cnt = 0;
if(root == null) return 0;
if(root.val == sum) cnt++;
return cnt + dfs(root.left,sum - root.val) + dfs(root.right,sum - root.val);
}

Delete Node in a BST

思路:判断key和头结点的val,利用二叉查找树的特点,递归查找直到它们相等;然后分为三种情况:
1.若左右子树都不为空,则找到右子树中的最小值赋给root,然后像右删除该节点
2.若左子树为空,则root = root.right
3.若右子树为空,则root = root.left public TreeNode deleteNode(TreeNode root, int key) {
if(root == null) return null;
if(root.val > key){
root.left = deleteNode(root.left,key);
}
else if(root.val < key){
root.right = deleteNode(root.right,key);
}
else{
if(root.left != null && root.right != null){
TreeNode node = getMin(root);
root.val = node.val;
root.right = deleteNode(root.right,root.val);
}
else if(root.left == null) root = root.right;
else root = root.left;
}
return root;
} public TreeNode getMin(TreeNode root){
TreeNode ptr = root.right;
while(ptr.left != null){
ptr = ptr.left;
}
return ptr;
}

Sum Root to Leaf Numbers

思路:依次往下计算总和,注意如果遇到不好计算的变量,考虑将其作为参数传入  

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

117. Populating Next Right Pointers in Each Node II constant space另解

TreeLinkNode dummyHead = new TreeLinkNode(0);
TreeLinkNode pre = dummyHead;
while (root != null) {
if (root.left != null) {
pre.next = root.left;
pre = pre.next;
}
if (root.right != null) {
pre.next = root.right;
pre = pre.next;
}
root = root.next;
if (root == null) {
pre = dummyHead;
root = dummyHead.next;
dummyHead.next = null;
}
}
总结
102/107/103/199/116/117	利用队列,层次遍历
**404 递归判断,只要是左叶子节点,则将val加起来
**257 递归判断,若是叶子节点则只需连接val,入list返回即可,若不是则需连接val加->
100 递归判断头结点是否一致或是否都为空
235 判断两节点与头结点的大小,利用二叉查找树的特点
110 需要计算左子树和右子树的高度判断是否是平衡二叉树,递归
112 只要叶子节点等于target则返回true,递归
226 先翻转头结点的左右子树,然后递归将左右子树分别最为头结点翻转
104 计算树的最大高度
111 计算树的最小高度,注意当左子树为空时,高度应为1+右子树高度,而不是1,右子树为空亦然
101 是226和100的综合,将左子树完全翻转以后看和右子树是不是一致
230/98 中序遍历
108 每次找中间的数作为头结点,递归
94/144/145 树的三种遍历
训练
**Serialize and Deserialize BST :保存前向遍历结果,利用二叉查找树的特点,小于头结点的放一边,大于头结点的放一边,遍历
House Robber III :计算每个节点的最大rob值并保存在map中避免重复计算
**Lowest Common Ancestor of a Binary Tree :分别找到两个节点的路径,然后依次比较
Count Complete Tree Nodes :计算左高度和右高度是否一致,不一致则递归计算
**Binary Search Tree Iterator:利用栈首先将左子树都入栈,接着查找next smallest
**Flatten Binary Tree to Linked List:注意左右子树都要赋值
Unique Binary Search Trees:左子树和右子树的种类相乘为一个顶点的所有情况
**Recover Binary Search Tree:中序遍历,找到那两个节点,然后交换
Serialize and Deserialize Binary Tree:前序遍历将null也序列化进去,然后利用队列建树(q.addAll(Arrays.asList(data.split(",")));)
提示
若递归以后的结果直接是最后的结果则直接在主函数内进行,如计数
若需要将递归以后的结果放入list,则需将list作为参数传递给另一函数,在此函数中进行递归 常用:
计算树的高度
四种遍历方式(前序、中序、后序、层次)
在二分查找树的题中,想到中序遍历
java队列的使用
add        增加一个元索                如果队列已满,则抛出一个IIIegaISlabEepeplian异常
remove 移除并返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常
offer 添加一个元素并返回true 如果队列已满,则返回false
poll 移除并返问队列头部的元素 如果队列为空,则返回null
peek 返回队列头部的元素 如果队列为空,则返回null
put 添加一个元素 如果队列满,则阻塞
take 移除并返回队列头部的元素 如果队列为空,则阻塞
remove、element、offer 、poll、peek 其实是属于Queue接口。
java栈的使用
add/push	添加一个元素
pop 弹出一个元素
peek 查看首个元素,不移除

LeetCode----Tree的更多相关文章

  1. 814. Binary Tree Pruning(leetcode) (tree traverse)

    https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...

  2. leetcode tree相关题目总结

    leetcode tree相关题目小结 所使用的方法不外乎递归,DFS,BFS. 1. 题100 Same Tree Given two binary trees, write a function ...

  3. LeetCode & tree & binary tree

    LeetCode & tree & binary tree 树 & 二叉树 refs https://leetcode.com/problemset/all/?topicSlu ...

  4. [leetcode tree]107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  5. [leetcode tree]103. Binary Tree Zigzag Level Order Traversal

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

  6. [leetcode tree]102. Binary Tree Level Order Traversal

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

  7. [leetcode tree]101. Symmetric Tree

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

  8. [leetcode tree]98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. [leetcode tree]95. Unique Binary Search Trees II

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  10. [leetcode tree]104. Maximum Depth of Binary Tree

    求树的最大深度 class Solution(object): def maxDepth(self, root): if not root: return 0 left = self.maxDepth ...

随机推荐

  1. MVC 支持同名路由,不同命名空间

    有时候我们会碰到两个项目合在一起,那么必然会碰到两个同名的controller,其实MVC在注册路由,添加Route的时候可以指定当前规则解析那个命名空间下的所有Controller. 注:Contr ...

  2. Node相关参考资料

    参考资料: [玩转Nodejs日志管理log4js]http://blog.fens.me/nodejs-log4js/ [dependencies与devDependencies之间的区别]http ...

  3. Python学习路程-常用设计模式学习

    本节内容 设计模式介绍 设计模式分类 设计模式6大原则 1.设计模式介绍 设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复 ...

  4. $("").click与onclick的区别

    onclick是绑定事件,click本身是方法作用是触发onclick事件,只要执行了元素的click()方法,下面示例 Html代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  5. 基础小功能之(1)震动,(2)检测app是否在前台运行

    //开启震动 //添加权限<uses-permission android:name="android.permission.VIBRATE" /> private v ...

  6. vs 下安装boost

    首先到boost官网去下载最新的版本的boost库,选对对应的平台版本. 下载官网 解压文件, 先运行bootstrap.bat文件,生成bjam.exe并运行它.推荐完全编译的方式运行bjam 推荐 ...

  7. Java笔记10-Object包装类型字符串

    提纲: 1.java.lang.0bject中常用方法介绍 2.基本类型对应的包装类型的介绍 以及基本类型和包装类型之间的相互转换 3.java.lang.String 字符串处理类 java.lan ...

  8. Hive中的排序和分组(对map和reduce的影响,值得一看!)

    order by order by 会对输入做全局排序,因此只有一个reducer(多个reducer无法保证全局有序)只有一个reducer,会导致当输入规 模较大时,需要较长的计算时间. set ...

  9. mysql常用操作

    一.什么是数据库 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库. SQL( Structured Query Language)语言的全称是结构化查询语言.数据库管理系统通过S ...

  10. Groovy入门教程

    Groovy入门教程 kmyhy@126.com  2009-5-13 一.groovy是什么 简单地说,Groovy 是下一代的java语言,跟java一样,它也运行在 JVM 中. 作为跑在JVM ...