leetcode试题总结<8>

257. Binary Tree Paths

题意:给定一个二叉树,返回其从根到叶子节点的每一条路径

/**
 * 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: 
    void binaryTree(vector<string> &result,TreeNode* root, string str)
    {
        if(!root->left && !root->right)  //结点为叶节点
        {
            result.push_back(str);
            return;
        }
        
        if(root->left)
            binaryTree(result,root->left,str + "->" + to_string(root->left->val));
        if(root->right)
            binaryTree(result,root->right,str + "->" + to_string(root->right->val));
    }
    vector<string> binaryTreePaths(TreeNode* root) {
         vector<string> result;
         if(!root)
            return result;
         binaryTree(result,root,to_string(root->val));
         return result;
    }
};
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;

        if(!root) return res;
        else if(!root->left && !root->right) res.push_back(to_string(root->val));
        
        string head = to_string(root->val) + "->";
        
        for(auto item: binaryTreePaths(root->left)) res.push_back(head + item);
        for(auto item: binaryTreePaths(root->right)) res.push_back(head + item);
        
        return res;
    }

总结:从根节点开始,利用递归的方法将每个结点的字符信息保存到vector容器中。


235. Lowest Common Ancestor of a Binary Search Tree
题意:给定一个二叉搜索树,找出两个给定结点的最小公共祖先。

/**
 * 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:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(!root || !p || !q)
            return NULL;
        if(root->val > p->val && root->val > q->val)
            return lowestCommonAncestor(root->left,p,q);
        else if(root->val < p->val && root->val < q->val)
            return  lowestCommonAncestor(root->right,p,q);
        else 
            return root;
    }
};
总结:因为是二叉搜索树,所以结点的大小顺序已经是排好的了,所以这里可用递归思想:

          1.如果根节点或给定的结点q、p之一为空,则返回空。

          2.判断p,q与根节点的大小关系,如果二者都小于根节点,说明最小公共祖先位于根节点的左子树上,因此将根              结点换成其左子树结点进行迭代。如果二者都大于根结点,说明最小公共祖先位于根节点的右子树上,因此                将根结点换成其右子树结点进行迭代。如果去p,q一个大于根结点,一个小于跟结点,说明最小公共祖先就是              跟结点,返回跟结点即可。

226. Invert Binary Tree

题意:给定一个二叉树,使整个二叉树的所有结点的左右结点反转。

// My answer
/**
 * 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:
    TreeNode* invertTree(TreeNode* root) {
        if(!root )
            return root;
        if(root->left || root->right)
        {
            TreeNode* temp = root->left;
            root->left = root->right;
            root->right = temp;
            
            invertTree(root->left);
            invertTree(root->right);
        }
        return root;
    }
};
Recursive

TreeNode* invertTree(TreeNode* root) {
    if (root) {
        invertTree(root->left);
        invertTree(root->right);
        std::swap(root->left, root->right);
    }
    return root;
}
Non-Recursive

TreeNode* invertTree(TreeNode* root) {
    std::stack<TreeNode*> stk;
    stk.push(root);
    
    while (!stk.empty()) {
        TreeNode* p = stk.top();
        stk.pop();
        if (p) {
            stk.push(p->left);
            stk.push(p->right);
            std::swap(p->left, p->right);
        }
    }
    return root;
}
总结:用递归的方法,始终有一种摸不着套路的感觉。这道题递归的思路应该算是比较清晰的,一次交换每一层每一个结点的左右结点即可。答案中还给出了一种利用栈的迭代的方法。

112. Path Sum

题意:给定一个二叉树及一个值,判断二叉树中有无从根节点到叶子节点的路径的和等于给定的值,若有返回true,无返回false.

/**
 * 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:
    bool hasPathSum(TreeNode* root, int sum) {
       if(root == NULL)
           return false;
       if(root->val == sum && root->left == NULL && root->right == NULL)
           return true;
       return hasPathSum(root->left,sum - root->val) || hasPathSum(root->right,sum - root->val);
    }
    
};
class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        
        if(root==NULL)
            return false;
           
        if(root->left!=NULL || root->right!=NULL)
            return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val);
        else
            return sum-root->val==0 ? true:false;
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值