目录
513 找树左下角的值
迭代
class Solution {
public int findBottomLeftValue(TreeNode root) {
int res = 0;
Deque<TreeNode> 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;
Deque<TreeNode>st = 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)
112 路径总和
迭代
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null)return false;
Deque<TreeNode>st1 = new LinkedList<>();
Deque<Integer>st2 = 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;
Deque<TreeNode>st1 = new LinkedList<>();
Deque<Integer>st2 = 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)
113 路径总和 II
class Solution {
List<List<Integer>>res = new ArrayList<>();
List<Integer>path = new LinkedList<>();
public List<List<Integer>> 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 {
Map<Integer,Integer>mp;
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length == 0 || inorder == null)return null;
mp = new HashMap<>();
for(int i = 0;i < inorder.length;i++){
mp.put(inorder[i],i);
}
return buildHelper(inorder,0,inorder.length,postorder,0,postorder.length);
}
private TreeNode buildHelper(int[] inorder,int inStart,int inEnd,int[] postorder,int postStart,int postEnd){
//范围均为前闭后开
if(postStart >= postEnd || inStart >= inEnd)return null;
int mid = mp.get(postorder[postEnd - 1]);
TreeNode cur = new TreeNode(inorder[mid]);
int len = mid - inStart;
cur.left = buildHelper(inorder,inStart,mid,postorder,postStart,len + postStart);
cur.right = buildHelper(inorder,mid + 1,inEnd,postorder,len + postStart,postEnd - 1);
return cur;
}
}
时间复杂度O(n)
空间复杂度O(n)
105 从前序与中序遍历序列构造二叉树
class Solution {
Map<Integer,Integer>mp;
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder == null || preorder.length == 0)return null;
mp = new HashMap<>();
for(int i = 0;i < inorder.length;i++){
mp.put(inorder[i],i);
}
return buildHelper(preorder,0,preorder.length,inorder,0,inorder.length);
}
private TreeNode buildHelper(int[] preorder,int preStart,int preEnd,int[] inorder,int inStart,int inEnd){
if(preStart >= preEnd || inStart >= inEnd)return null;
int mid = mp.get(preorder[preStart]);
TreeNode cur = new TreeNode(inorder[mid]);
int prelen = mid - inStart;
cur.left = buildHelper(preorder,preStart + 1,preStart + prelen + 1,inorder,inStart,mid);
cur.right = buildHelper(preorder,preStart + prelen + 1,preEnd,inorder,mid + 1,inEnd);
return cur;
}
}
时间复杂度O(n)
空间复杂度O(n)