目录
513 找树左下角的值
迭代
递归
112 路径总和
迭代
递归
113 路径总和 II
106 从中序与后序遍历序列构造二叉树
105 从前序与中序遍历序列构造二叉树
class Solution {
public int findBottomLeftValue(TreeNode root) {
int res = 0;
Deque st = new LinkedList<>();
st.add(root);
while(!st.isEmpty()){
int siz = st.size();
for(int i = 0;i < siz;i++){
TreeNode cur = st.pop();
if(i == 0)res = cur.val;
if(cur.left != null)st.add(cur.left);
if(cur.right != null)st.add(cur.right);
}
}
return res;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public int findBottomLeftValue(TreeNode root) {
int res = 0;
Dequest = new LinkedList<>();
st.add(root);
while(!st.isEmpty()){
TreeNode cur = st.pop();
if(cur.right != null)st.add(cur.right);
if(cur.left != null)st.add(cur.left);//确保左边最后被遍历到
res = cur.val;
}
return res;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
private int value = 0;
private int depth = 0;
public int findBottomLeftValue(TreeNode root) {
value = root.val;
findLeftValue(root,0);
return value;
}
private void findLeftValue(TreeNode root,int dep){
if(root.left == null && root.right == null){
if(dep > depth){
depth = dep;
value = root.val;
}
}
if(root.left != null)findLeftValue(root.left,dep + 1);
if(root.right != null)findLeftValue(root.right,dep + 1);
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null)return false;
Dequest1 = new LinkedList<>();
Dequest2 = new LinkedList<>();
st1.add(root);
st2.add(root.val);
while(!st1.isEmpty()){
int siz = st1.size();
for(int i = 0;i < siz;i++){
int sum = st2.pop();
TreeNode cur = st1.pop();
if(cur.left == null && cur.right == null && sum == targetSum)return true;
if(cur.right != null){
st1.add(cur.right);
st2.add(cur.right.val + sum);
}
if(cur.left != null){
st1.add(cur.left);
st2.add(cur.left.val + sum);
}
}
}
return false;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null)return false;
Dequest1 = new LinkedList<>();
Dequest2 = new LinkedList<>();
st1.add(root);
st2.add(root.val);
while(!st1.isEmpty()){
TreeNode cur = st1.pop();
int sum = st2.pop();
if(cur.left == null && cur.right == null && sum == targetSum)return true;
if(cur.right != null){
st1.add(cur.right);
st2.add(cur.right.val + sum);
}
if(cur.left != null){
st1.add(cur.left);
st2.add(cur.left.val + sum);
}
}
return false;
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null)return false;
if(root.left == null && root.right == null)return root.val == targetSum;
return hasPathSum(root.left,targetSum - root.val) || hasPathSum(root.right,targetSum - root.val);
}
}
时间复杂度O(n)
空间复杂度O(n)
class Solution {
List>res = new ArrayList<>();
Listpath = new LinkedList<>();
public List> pathSum(TreeNode root, int targetSum) {
if(root == null)return res;
dfs(root,targetSum);
return res;
}
private void dfs(TreeNode root,int targetSum){
path.add(root.val);
if(root.left == null && root.right == null){
if(root.val == targetSum){
res.add(new ArrayList<>(path));
}
return;
}
if(root.left != null){
dfs(root.left,targetSum - root.val);
path.remove(path.size() - 1);
}
if(root.right != null){
dfs(root.right,targetSum - root.val);
path.remove(path.size() - 1);
}
}
}
时间复杂度O(n^2)
空间复杂度O(n)
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length == 0 || inorder == null)return null;
return buildHelper(inorder,0,inorder.length,postorder,0,postorder.length);
}
private TreeNode buildHelper(int[] inorder,int inorderFirst,int inorderEnd,int[] postorder,int postorderFirst,int postorderEnd){
if(postorderFirst == postorderEnd)return null;
int val = postorder[postorderEnd - 1];
TreeNode cur = new TreeNode(val);
int mid = inorderFirst;
for(;mid < inorderEnd;mid++){
if(inorder[mid] == cur.val)break;
}
//将inorder分为左部分,右部分
int leftinorderFirst = inorderFirst;
int leftinorderEnd = mid;
int rightinorderFirst = mid + 1;
int rightinorderEnd = inorderEnd;
//将postorder分为左部分,右部分
int leftpostorderFirst = postorderFirst;
int leftpostorderEnd = leftpostorderFirst + mid - inorderFirst;
int rightpostorderFirst = leftpostorderEnd;
int rightpostorderEnd = postorderEnd - 1;
cur.left = buildHelper(inorder,leftinorderFirst,leftinorderEnd,postorder,leftpostorderFirst,leftpostorderEnd);
cur.right = buildHelper(inorder,rightinorderFirst,rightinorderEnd,postorder,rightpostorderFirst,rightpostorderEnd);
return cur;
}
}
时间复杂度O(n)
空间复杂度O(n)