leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree

1、



Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:

Given the below binary tree and sum = 22,

              5
             /             4   8
           /   /           11  13  4
         /  \              7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which
sum is 22.

分析,此题比较简单,采用深度优先策略。

代码如下:

class Solution {
public:
    bool hasPathSum(TreeNode *root, int sum) {
        hasPath = false;
        if(root){
            int tempSum = 0;
            pathSumCore(root,tempSum,sum);
        }
        return hasPath;
    }
    void pathSumCore(TreeNode *root,int tempSum, int sum){
        if(hasPath){
            return;
        }
        tempSum += root->val;
        if(!root->left&&!root->right){
            if(tempSum == sum){
                hasPath = true;
            }
            return;
        }
        if(root->left){
            pathSumCore(root->left,tempSum,sum);
        }
         if(root->right){
            pathSumCore(root->right,tempSum,sum);
        }
    }
    bool hasPath;
};

2、Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path‘s sum equals the given sum.

For example:

Given the below binary tree and sum = 22,

              5
             /             4   8
           /   /           11  13  4
         /  \    /         7    2  5   1

return

[
   [5,4,11,2],
   [5,8,4,5]
]

分析:此题也很简单,常见题,跟上述不同的是保存路径。

代码:

class Solution {
public:
    vector<vector<int> > pathSum(TreeNode *root, int sum) {
        if(root){
            int tempSum = 0;
            vector<int> path;
            pathSumCore(root,path,tempSum,sum);
        }
        return pathVec;
    }
    void pathSumCore(TreeNode* root,vector<int> path,int tempSum,int sum){
        tempSum += root->val;
        path.push_back(root->val);
        if(!root->left && !root->right){
            if(tempSum == sum){
                pathVec.push_back(path);
            }
            return;
        }
        if(root->left){
            pathSumCore(root->left,path,tempSum,sum);
        }
        if(root->right){
            pathSumCore(root->right,path,tempSum,sum);
        }
    }
    vector<vector<int> > pathVec;
};

3、Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.

For example,

Given

         1
        /        2   5
      / \        3   4   6

The flattened tree should look like:

   1
         2
             3
                 4
                     5
                         6

click to show hints.

Hints:

If you notice carefully in the flattened tree, each node‘s right child points to the next node of a pre-order traversal.

分析:上述提示可以看出,相当于二叉树的先序遍历,对每一个结点,先访问根结点,再先序遍历左子树,串在根结点的右孩子上,再先序遍历原右子树,继续串在右孩子上。

代码如下:

class Solution {
public:
    void flatten(TreeNode *root) {
        if(root){
            preOrder(root);
        }
    }
    TreeNode *preOrder(TreeNode *root){
        if(!root->left && !root->right){
            return root;
        }
        TreeNode *lastNode = NULL;
        TreeNode *rightNode = root->right;
        //TreeNode *leftNode = root->left;
        if(root->left){
            root->right = root->left;
            lastNode = preOrder(root->left);
            root->left = NULL;
            lastNode->right = rightNode;
        }
        if(rightNode){
            lastNode = preOrder(rightNode);
        }
        return lastNode;
    }
};

4、Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

分析:此题很简单,递归。

class Solution {
public:
    int minDepth(TreeNode *root) {
        if(!root){
            return 0;
        }
        n_minDepth = INT_MAX;
        int tempDepth = 0;
        minDepthCore(root,tempDepth);
        return n_minDepth;
    }
    void minDepthCore(TreeNode *root,int tempDepth){
        ++tempDepth;
        if(tempDepth >= n_minDepth){
            return;
        }
        if(!root->left && !root->right){
            if(tempDepth < n_minDepth){
                n_minDepth = tempDepth;
            }
            return;
        }
        if(root->left){
            minDepthCore(root->left,tempDepth);
        }
        if(root->right){
            minDepthCore(root->right,tempDepth);
        }
    }
    int n_minDepth;
};

leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree

时间: 2024-08-06 11:56:20

leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree的相关文章

[Leetcode][JAVA] Path Sum I &amp;&amp; II

Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return t

LeetCode Solutions : Path Sum I &amp; II

Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4   8 /   / \ 11  13  4 /  \  

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

[LeetCode] 437. Path Sum III_ Easy tag: DFS

You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. 中文:给定一颗二叉树,如

【LeetCode】Path Sum

题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true,

Minimum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题解: 递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth. 递归解法: 1     public 

[leetcode]Minimum Depth of Binary Tree @ Python

原题地址:http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 题意: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 解题思路:分几种情况考虑:1,树为空,

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1