提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
``
``
迭代
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int findBottomLeftValue(TreeNode root) {
Deque<TreeNode> deq = new LinkedList<>();
deq.offerLast(root);
TreeNode cur = null;
while(!deq.isEmpty()){
cur = deq.peekFirst();
int len = deq.size();
for(int i = 0; i < len; i ++){
TreeNode p = deq.pollFirst();
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
}
return cur.val;
}
}
递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int depth = 0;
TreeNode node = null;
public int findBottomLeftValue(TreeNode root) {
fun(root, 1);
return node.val;
}
public void fun(TreeNode root, int cur){
if(cur > depth){
node = root;
}
depth = depth > cur ? depth : cur;
if(root.left != null){
fun(root.left, cur + 1);
}
if(root.right != null){
fun(root.right, cur + 1);
}
}
}
递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
boolean flag = false;
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null){
return flag;
}
fun(root, targetSum, root.val);
return flag;
}
public void fun(TreeNode root, int targetSum, int path){
if(root.left == null && root.right == null){
if(targetSum == path){
flag = true;
}
}
if(root.left != null){
fun(root.left, targetSum, path + root.left.val);
}
if(root.right != null){
fun(root.right, targetSum, path + root.right.val);
}
}
}
迭代
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
Deque<TreeNode> deq1 = new LinkedList<>();
Deque<Integer> deq2 = new LinkedList<>();
if(root == null)return false;
deq1.offerLast(root);
deq2.offerLast(root.val);
while(!deq1.isEmpty()){
int len = deq1.size();
for(int i = 0; i < len; i ++){
TreeNode p = deq1.pollLast();
int sum = deq2.pollLast();
if(p.left == null && p.right == null && sum == targetSum)return true;
if(p.right != null){
deq1.offerLast(p.right);
deq2.offerLast(sum + p.right.val);
}
if(p.left != null){
deq1.offerLast(p.left);
deq2.offerLast(sum + p.left.val);
}
}
}
return false;
}
}
递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
if(root == null)return res;
List<Integer> path = new ArrayList<>();
path.add(root.val);
fun(root, targetSum, root.val, path);
return res;
}
public void fun(TreeNode root, int targetSum, int cur, List<Integer> path){
if(root.left == null && root.right == null && cur == targetSum){
res.add(new ArrayList<>(path));
}
if(root.left != null){
path.add(root.left.val);
fun(root.left, targetSum, cur + root.left.val, path);
path.remove(path.size() - 1);
}
if(root.right != null){
path.add(root.right.val);
fun(root.right, targetSum, cur + root.right.val, path);
path.remove(path.size() - 1);
}
}
}
迭代
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> result = new ArrayList<>();
Stack<TreeNode> nodeStack = new Stack<>();
Stack<Integer> sumStack = new Stack<>();
Stack<ArrayList<Integer>> pathStack = new Stack<>();
if(root == null)
return result;
nodeStack.add(root);
sumStack.add(root.val);
pathStack.add(new ArrayList<>());
while(!nodeStack.isEmpty()){
TreeNode currNode = nodeStack.peek();
int currSum = sumStack.pop();
ArrayList<Integer> currPath = pathStack.pop();
if(currNode != null){
nodeStack.pop();
nodeStack.add(currNode);
nodeStack.add(null);
sumStack.add(currSum);
currPath.add(currNode.val);
pathStack.add(new ArrayList(currPath));
if(currNode.right != null){
nodeStack.add(currNode.right);
sumStack.add(currSum + currNode.right.val);
pathStack.add(new ArrayList(currPath));
}
if(currNode.left != null){
nodeStack.add(currNode.left);
sumStack.add(currSum + currNode.left.val);
pathStack.add(new ArrayList(currPath));
}
}else{
nodeStack.pop();
TreeNode temp = nodeStack.pop();
if(temp.left == null && temp.right == null && currSum == targetSum)
result.add(new ArrayList(currPath));
}
}
return result;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return fun(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1);
}
public TreeNode fun(int[] inorder, int[] postorder, int inStart, int inEnd, int postStart,
int postEnd){
if(postStart > postEnd)return null;
TreeNode cur = new TreeNode(postorder[postEnd]);
int index = 0, leftLen = 0, rightLen = 0;
for(int i = inStart; i <= inEnd; i ++){
if(inorder[i] == postorder[postEnd]){
index = i;
leftLen = index-inStart;
rightLen = inEnd-index;
}
}
TreeNode leftChild = fun(inorder, postorder, inStart, index-1, postStart, postStart+leftLen-1);
TreeNode rightChild = fun(inorder, postorder, index+1, inEnd, postStart+leftLen, postEnd-1); cur.left = leftChild;
cur.right = rightChild;
return cur;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
Map<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
for(int i = 0; i < inorder.length; i ++){
map.put(inorder[i], i);
}
return fun(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
}
public TreeNode fun(int[] preorder, int[] inorder, int preStart, int preEnd, int inStart, int inEnd){
if(preStart > preEnd)return null;
TreeNode cur = new TreeNode(preorder[preStart]);
int index = map.get(preorder[preStart]);
int leftLen = index - inStart;
int rightLen = inEnd - index;
TreeNode leftChild = fun(preorder, inorder, preStart+1, preStart+leftLen, inStart, index-1);
TreeNode rightChild = fun(preorder, inorder, preStart+leftLen+1, preStart+leftLen+rightLen, index+1, index+rightLen);
cur.left = leftChild;
cur.right = rightChild;
return cur;
}
}