LeetCode OJ 之 Binary Tree Paths(二叉树路径)

题目:

Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

   1
 /   2     3
   5

All root-to-leaf paths are:

["1->2->5", "1->3"]

思路:

前序递归即可。

代码1:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root)
    {
        vector<string> result;
        if(root == NULL)
            return result;
        string path;
        binaryTreePaths(root , result , path);
        return result;
    }
    void binaryTreePaths(TreeNode* root , vector<string> &result , string path)
    {
        if(root == NULL)
            return;
        if(path.empty())
            path += to_string(root->val);
        else
        {
            path += "->";
            path += to_string(root->val);
        }
        if(root->left == NULL && root->right == NULL)
        {
            result.push_back(path);
            return;
        }

        binaryTreePaths(root->left , result , path);
        binaryTreePaths(root->right , result , path);
    }
};

代码2:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root)
    {
        vector<string> result;
        if(root == NULL)
            return result;
        string path;
        binaryTreePaths(root , result , path);
        return result;
    }
    void binaryTreePaths(TreeNode* root , vector<string> &result , string path)
    {
        if(root == NULL)
            return;

        if(root->left == NULL && root->right == NULL)
        {
            result.push_back(path + to_string(root->val));
            return;
        }

        binaryTreePaths(root->left , result , path + to_string(root->val) + "->");
        binaryTreePaths(root->right , result , path + to_string(root->val) + "->");
    }
};

版权声明:转载请注明出处。

时间: 2024-08-30 16:24:40

LeetCode OJ 之 Binary Tree Paths(二叉树路径)的相关文章

leetCode 257. Binary Tree Paths 二叉树路径

257. Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree:    1  /   2     3    5 All root-to-leaf paths are: ["1->2->5", "1->3"] 思路: 1.采用二叉树的后序遍历非递归版 2.在叶子节点的时候处理字

[LintCode] Binary Tree Paths 二叉树路径

Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2     3 \  5 All root-to-leaf paths are: [  "1->2->5",  "1->3"] LeetCode上的原题,请参见我之前的博客Binary Tree Paths. 解法一: class Solution {

&lt;LeetCode OJ&gt; 257. Binary Tree Paths

257. Binary Tree Paths My Submissions Question Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["

LeetCode OJ:Binary Tree Paths(二叉树路径)

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 简单的遍历查找路径问题,代码如下: 1 /** 2 * Definition for a binary tree node. 3 * str

[LeetCode] Binary Tree Paths 二叉树路径

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"]

257. Binary Tree Paths 二叉树路径

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] # Definition for a binary tree node. # class TreeNode(object): # def _

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po

LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl