Leetcode513.找树左下角的值
题目链接:513. 找树左下角的值
C++:(迭代法,层序遍历)
几天不见层序遍历又差点忘了,循环结束标志为!que.empty(),int size = que.size()帮助父结点弹出队列。
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
vector<vector<int>> result;
queue<TreeNode*> que;
if(root != nullptr) que.push(root);
while(!que.empty())
{
int size = que.size();
vector<int> vec;
while(size--)
{
TreeNode *cur = que.front();
vec.push_back(cur->val);
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
result.push_back(vec);
}
int fin = result[result.size()-1][0];
return fin;
}
};
Python:(递归法)
保证优先左边搜索,然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值
class Solution:
def __init__(self, result=None, maxdepth=float('-inf')):
self.result = result
self.maxdepth = maxdepth
def diguizuo(self, root, depth):
if root.left == None and root.right == None:
if depth > self.maxdepth:
self.maxdepth = depth
self.result = root.val
if root.left:
self.diguizuo(root.left, depth+1)
if root.right:
self.diguizuo(root.right, depth+1)
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
self.diguizuo(root, 0)
return self.result
Leetcode112. 路径总和
题目链接:112. 路径总和
C++:
class Solution {
public:
bool traversal(TreeNode* node, int count){
if(node->left == nullptr && node->right == nullptr && count == 0)
return true;
if(node->left == nullptr && node->right == nullptr && count != 0)
return false;
if(node->left)
{
count -= node->left->val;
if(traversal(node->left, count)) return true;
count += node->left->val;
}
if(node->right)
{
count -= node->right->val;
if(traversal(node->right, count)) return true;
count += node->right->val;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root == nullptr) return false;
return traversal(root, targetSum - root->val);
}
};
Leetcode113.路径总和Ⅱ
题目链接: 113. 路径总和 II
Python:
class Solution:
def __init__(self):
self.result = []
self.path = []
def traversal(self, node, count):
if node.left == None and node.right == None and count == 0:
self.result.append(self.path[:])
return
if node.left == None and node.right == None:
return
if node.left:
count -= node.left.val
self.path.append(node.left.val)
self.traversal(node.left, count)
count += node.left.val
self.path.pop()
if node.right:
count -= node.right.val
self.path.append(node.right.val)
self.traversal(node.right, count)
count += node.right.val
self.path.pop()
return
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
self.result.clear()
self.path.clear()
if root == None:
return self.result
self.path.append(root.val)
self.traversal(root, targetSum - root.val)
return self.result
Leetcode106.从中序与后序遍历序列构造二叉树
题目链接:
C++:
class Solution {
public:
TreeNode* traversal (vector<int>& inorder, vector<int>& postorder) {
if (postorder.size() == 0) return NULL;
// 后序遍历数组最后一个元素,就是当前的中间节点
int rootValue = postorder[postorder.size() - 1];
TreeNode* root = new TreeNode(rootValue);
// 叶子节点
if (postorder.size() == 1) return root;
// 找到中序遍历的切割点
int delimiterIndex;
for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {
if (inorder[delimiterIndex] == rootValue) break;
}
// 切割中序数组
// 左闭右开区间:[0, delimiterIndex)
vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
// [delimiterIndex + 1, end)
vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );
// postorder 舍弃末尾元素
postorder.resize(postorder.size() - 1);
// 切割后序数组
// 依然左闭右开,注意这里使用了左中序数组大小作为切割点
// [0, leftInorder.size)
vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
// [leftInorder.size(), end)
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
root->left = traversal(leftInorder, leftPostorder);
root->right = traversal(rightInorder, rightPostorder);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) return NULL;
return traversal(inorder, postorder);
}
};
Leetcode105.从前序与中序遍历序列构造二叉树
题目链接:105. 从前序与中序遍历序列构造二叉树
Python:
class Solution:
def traversal(self, preorder, inorder):
if len(preorder) == 0:
return None
root = TreeNode(preorder[0])
if len(preorder) == 1:
return root
# 寻找index
# index = inorder.index(root.val)
for i in range(len(inorder)):
if inorder[i] == root.val:
index = i
break
# inorder切片
leftinorder = inorder[0:index]
rightinorder = inorder[index+1:]
# preorder切片
preorder = preorder[1:]
leftpreorder = preorder[0:len(leftinorder)]
rightpreorder = preorder[len(leftinorder):]
root.left = self.traversal(leftpreorder, leftinorder)
root.right = self.traversal(rightpreorder, rightinorder)
return root
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if len(preorder) == 0 or len(inorder) == 0:
return None
return self.traversal(preorder, inorder)