• 二叉树算法总结


    刷了几道二叉树的算法题,基本都可以用递归求出来,在可以使用回溯法的题目中,回溯法的时间开销比递归少。

    递归调用分为两类:1.在根节点到叶子节点的路径中找出满足条件的值

             2.在任意两个节点之间寻找满足条件的路径

    根节点到叶子节点的路径选择

    leetcode上有类似的题目,

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

    An example is the root-to-leaf path 1->2->3 which represents the number 123.

    Find the total sum of all root-to-leaf numbers.

    For example,

        1
       / 
      2   3
    

    The root-to-leaf path 1->2 represents the number 12.
    The root-to-leaf path 1->3 represents the number 13.

    Return the sum = 12 + 13 = 25.

    思路一:可以使用递归调用,从根节点将需要保存的内容保存起来,判断到达叶子节点则判断该路径是否满足题目要求

     
     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     int sumNumbers(TreeNode* root) {
    13         vector<int> sumVec;
    14         int result;
    15         int sum = 0;
    16         if(root==NULL){
    17             return 0;
    18         }
    19         sumHelper(root,sumVec,result);
    20         for(int i = 0;i<sumVec.size();i++){
    21             sum+=sumVec[i];
    22         }
    23         return sum;
    24     }
    25     int sumHelper(TreeNode* root,vector<int>& sumVec,int result){
    26         if(!root->left&&!root->right){
    27             result = result*10+(root->val);
    28             sumVec.push_back(result);
    29         }
    30         if(root->left){
    31              int resultl = result;
    32              resultl = resultl*10+(root->val);
    33              sumHelper(root->left,sumVec,resultl);
    34         }
    35         if(root->right){
    36             result = result*10+(root->val);
    37             sumHelper(root->right,sumVec,result);
    38         }
    39     }
    40 };

    思路二,回溯法

    任意两个节点间的路径选择

    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 example:
    Given the below binary tree,

           1
          / 
         2   3
    

    Return 6.

    使用递归,使用全局变量MaxSum保存历史最大值,以root为起始扩散点的目的是将所有路径遍历完。

    注意:节点的值为负数时,不用将其加入路径中。

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11     int maxSum;
    12 public:
    13     int maxPathSum(TreeNode* root) {
    14         maxSum = -99999;
    15         if(root==NULL){return 0;}
    16         DFS(root);
    17         return maxSum;
    18     }
    19     int DFS(TreeNode* root){
    20         if(root==NULL) return 0;
    21         int sum = root->val;
    22         int l = DFS(root->left);
    23         int r = DFS(root->right);
    24         if(l>0){
    25             sum+=l;
    26         }
    27         if(r>0){
    28             sum+=r;
    29         }
    30        // int sum = l+r+root->val;
    31         maxSum = max(sum,maxSum);
    32         return (root->val+max(max(l,0),max(r,0)));
    33     }
    34 };
  • 相关阅读:
    第五章总结
    第二章总结
    第一章、基础知识总结
    实验 9 根据材料编程
    实验5
    汇编实验4
    实验 3 编程、编译、连接、跟踪
    实验 2 用机器指令和汇编指令编程
    汇编-实验一
    react面试笔录
  • 原文地址:https://www.cnblogs.com/timesdaughter/p/5323544.html
Copyright © 2020-2023  润新知