Day18 513.找树左下角的值 112. 路径总和 113.路径总和ii 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树

513.找树左下角的值 

Find Bottom Left Tree Value    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        #my idea:应该要用层序遍历,这样遍历到最后一层,取最左节点的值
        #answer = 0
        #每次用当前层的首个节点来更新 ans,当 BFS 结束后,ans 存储的是最后一层最靠左的节点。
        #queue = [root] 不能这么写,这么写就是list,list是不具备popleft attribute的
        queue = deque([root])
        ans = 0 #答案是最后一层的第一个节点的值
        while queue:
            ans = queue[0].val
            for _ in range(len(queue)): #pooleft是用于collections的deque中,弹出队首的数
                node = queue.popleft() 
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        return ans

112. 路径总和

    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        #①用递归做
        if not root:
            return False
        if root and not root.left and not root.right:
            if targetSum == root.val:#如果只有一个节点,那么这个点就是叶子节点,如果等于target就是true
                return True
        targetSum = targetSum - root.val 
        return self.hasPathSum(root.left,targetSum) or self.hasPathSum(root.right,targetSum) 
    #这句code是一个boolean,如果left返回false,它就会去右边recurse 
    #整棵树里只要有一条path走得通就行

113.路径总和ii

    def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
        #my idea:和112类似,只是需要遍历所有节点,记录下path
        #走到最下面一层,也就是左右节点都是空,然后判断targetsum是否为0,是0就是我们要找的path
        
        #照抄的递归方式实现
        res,temp = [],[]
        def pathsum1(root,target):
            if not root:
                return 
            if root.left == None and root.right == None:
                if target == root.val:
                    temp.append(root.val)
                    res.append(temp[:])
                    temp.pop()
                return 
            if root.left:
                temp.append(root.val)
                target = target - root.val
                pathsum1(root.left,target)
                target += root.val
                temp.pop()
            if root.right:
                temp.append(root.val)
                target -= root.val
                pathsum1(root.right,target)
                target += root.val
                temp.pop()
        pathsum1(root,targetSum)
        return res

105.从前序与中序遍历序列构造二叉树

不懂,以后再看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树的存储与遍: 1.初始化二叉树 ```c++ #include <iostream> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; TreeNode* createTree() { int val; cin >> val; if (val == -1) { return NULL; } TreeNode* root = new TreeNode(val); root->left = createTree(); root->right = createTree(); return root; } ``` 2.先叉树 ```c++ void preOrder(TreeNode* root) { if (root == NULL) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } ``` 3.叉树 ```c++ void inOrder(TreeNode* root) { if (root == NULL) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } ``` 4.后叉树 ```c++ void postOrder(TreeNode* root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } ``` 5.销毁二叉树 ```c++ void destroyTree(TreeNode* root) { if (root == NULL) { return; } destroyTree(root->left); destroyTree(root->right); delete root; } ``` 二叉树的复原: 1.由前序列确定复原二叉树 ```c++ TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { if (preorder.empty() || inorder.empty()) { return NULL; } int rootVal = preorder[0]; TreeNode* root = new TreeNode(rootVal); vector<int>::iterator it = find(inorder.begin(), inorder.end(), rootVal); int leftSize = it - inorder.begin(); vector<int> leftPreorder(preorder.begin() + 1, preorder.begin() + 1 + leftSize); vector<int> leftInorder(inorder.begin(), it); vector<int> rightPreorder(preorder.begin() + 1 + leftSize, preorder.end()); vector<int> rightInorder(it + 1, inorder.end()); root->left = buildTree(leftPreorder, leftInorder); root->right = buildTree(rightPreorder, rightInorder); return root; } ``` 2.由、后序列确定复原二叉树 ```c++ TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { if (inorder.empty() || postorder.empty()) { return NULL; } int rootVal = postorder.back(); TreeNode* root = new TreeNode(rootVal); vector<int>::iterator it = find(inorder.begin(), inorder.end(), rootVal); int leftSize = it - inorder.begin(); vector<int> leftInorder(inorder.begin(), it); vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftSize); vector<int> rightInorder(it + 1, inorder.end()); vector<int> rightPostorder(postorder.begin() + leftSize, postorder.end() - 1); root->left = buildTree(leftInorder, leftPostorder); root->right = buildTree(rightInorder, rightPostorder); return root; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值