日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
做了這么多題目了,會求左葉子之和么?

本文轉(zhuǎn)載自微信公眾號「代碼隨想錄」,作者程序員Carl 。轉(zhuǎn)載本文請聯(lián)系代碼隨想錄公眾號。

左葉子之和

題目地址:https://leetcode-cn.com/problems/sum-of-left-leaves/

計算給定二叉樹的所有左葉子之和。

示例:

思路

首先要注意是判斷左葉子,不是二叉樹左側(cè)節(jié)點,所以不要上來想著層序遍歷。

因為題目中其實沒有說清楚左葉子究竟是什么節(jié)點,那么我來給出左葉子的明確定義:如果左節(jié)點不為空,且左節(jié)點沒有左右孩子,那么這個節(jié)點就是左葉子

大家思考一下如下圖中二叉樹,左葉子之和究竟是多少?

其實是0,因為這棵樹根本沒有左葉子!

那么判斷當前節(jié)點是不是左葉子是無法判斷的,必須要通過節(jié)點的父節(jié)點來判斷其左孩子是不是左葉子。

如果該節(jié)點的左節(jié)點不為空,該節(jié)點的左節(jié)點的左節(jié)點為空,該節(jié)點的左節(jié)點的右節(jié)點為空,則找到了一個左葉子,判斷代碼如下:

  
 
 
 
  1. if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) { 
  2.     左葉子節(jié)點處理邏輯 

遞歸法

遞歸的遍歷順序為后序遍歷(左右中),是因為要通過遞歸函數(shù)的返回值來累加求取左葉子數(shù)值之和。。

遞歸三部曲:

1.確定遞歸函數(shù)的參數(shù)和返回值

判斷一個樹的左葉子節(jié)點之和,那么一定要傳入樹的根節(jié)點,遞歸函數(shù)的返回值為數(shù)值之和,所以為int

使用題目中給出的函數(shù)就可以了。

2.確定終止條件

依然是

  
 
 
 
  1. if (root == NULL) return 0; 

3.確定單層遞歸的邏輯

當遇到左葉子節(jié)點的時候,記錄數(shù)值,然后通過遞歸求取左子樹左葉子之和,和 右子樹左葉子之和,相加便是整個樹的左葉子之和。

代碼如下:

  
 
 
 
  1. int leftValue = sumOfLeftLeaves(root->left);    // 左 
  2. int rightValue = sumOfLeftLeaves(root->right);  // 右 
  3.                                                 // 中 
  4. int midValue = 0; 
  5. if (root->left && !root->left->left && !root->left->right) { 
  6.     midValue = root->left->val; 
  7. int sum = midValue + leftValue + rightValue; 
  8. return sum; 

整體遞歸代碼如下:

  
 
 
 
  1. class Solution { 
  2. public: 
  3.     int sumOfLeftLeaves(TreeNode* root) { 
  4.         if (root == NULL) return 0; 
  5.  
  6.         int leftValue = sumOfLeftLeaves(root->left);    // 左 
  7.         int rightValue = sumOfLeftLeaves(root->right);  // 右 
  8.                                                         // 中 
  9.         int midValue = 0; 
  10.         if (root->left && !root->left->left && !root->left->right) { // 中 
  11.             midValue = root->left->val; 
  12.         } 
  13.         int sum = midValue + leftValue + rightValue; 
  14.         return sum; 
  15.     } 
  16. }; 

以上代碼精簡之后如下:

  
 
 
 
  1. class Solution { 
  2. public: 
  3.     int sumOfLeftLeaves(TreeNode* root) { 
  4.         if (root == NULL) return 0; 
  5.         int midValue = 0; 
  6.         if (root->left != NULL && root->left->left == NULL && root->left->right == NULL) { 
  7.             midValue = root->left->val; 
  8.         } 
  9.         return midValue + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); 
  10.     } 
  11. }; 

迭代法

本題迭代法使用前中后序都是可以的,只要把左葉子節(jié)點統(tǒng)計出來,就可以了,那么參考文章 二叉樹:聽說遞歸能做的,棧也能做!和二叉樹:迭代法統(tǒng)一寫法中的寫法,可以寫出一個前序遍歷的迭代法。

判斷條件都是一樣的,代碼如下:

  
 
 
 
  1. class Solution { 
  2. public: 
  3.     int sumOfLeftLeaves(TreeNode* root) { 
  4.         stack st; 
  5.         if (root == NULL) return 0; 
  6.         st.push(root); 
  7.         int result = 0; 
  8.         while (!st.empty()) { 
  9.             TreeNode* node = st.top(); 
  10.             st.pop(); 
  11.             if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) { 
  12.                 result += node->left->val; 
  13.             } 
  14.             if (node->right) st.push(node->right); 
  15.             if (node->left) st.push(node->left); 
  16.         } 
  17.         return result; 
  18.     } 
  19. }; 

總結(jié)

這道題目要求左葉子之和,其實是比較繞的,因為不能判斷本節(jié)點是不是左葉子節(jié)點。

此時就要通過節(jié)點的父節(jié)點來判斷其左孩子是不是左葉子了。

平時我們解二叉樹的題目時,已經(jīng)習慣了通過節(jié)點的左右孩子判斷本節(jié)點的屬性,而本題我們要通過節(jié)點的父節(jié)點判斷本節(jié)點的屬性。

希望通過這道題目,可以擴展大家對二叉樹的解題思路。

其他語言版本

Java

遞歸

  
 
 
 
  1. class Solution { 
  2.     public int sumOfLeftLeaves(TreeNode root) { 
  3.         if (root == null) return 0; 
  4.         int leftValue = sumOfLeftLeaves(root.left);    // 左 
  5.         int rightValue = sumOfLeftLeaves(root.right);  // 右 
  6.                                                         
  7.         int midValue = 0; 
  8.         if (root.left != null && root.left.left == null && root.left.right == null) { // 中 
  9.             midValue = root.left.val; 
  10.         } 
  11.         int sum = midValue + leftValue + rightValue; 
  12.         return sum; 
  13.     } 

迭代

  
 
 
 
  1. class Solution { 
  2.     public int sumOfLeftLeaves(TreeNode root) { 
  3.         if (root == null) return 0; 
  4.         Stack stack = new Stack<> (); 
  5.         stack.add(root); 
  6.         int result = 0; 
  7.         while (!stack.isEmpty()) { 
  8.             TreeNode node = stack.pop(); 
  9.             if (node.left != null && node.left.left == null && node.left.right == null) { 
  10.                 result += node.left.val; 
  11.             } 
  12.             if (node.right != null) stack.add(node.right); 
  13.             if (node.left != null) stack.add(node.left); 
  14.         } 
  15.         return result; 
  16.     } 

Python

遞歸

  
 
 
 
  1. class Solution: 
  2.     def sumOfLeftLeaves(self, root: TreeNode) -> int: 
  3.         if not root:  
  4.             return 0 
  5.          
  6.         left_left_leaves_sum = self.sumOfLeftLeaves(root.left)  # 左 
  7.         right_left_leaves_sum = self.sumOfLeftLeaves(root.right) # 右 
  8.          
  9.         cur_left_leaf_val = 0 
  10.         if root.left and not root.left.left and not root.left.right:  
  11.             cur_left_leaf_val = root.left.val  # 中 
  12.              
  13.         return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum 

迭代

  
 
 
 
  1. class Solution: 
  2.     def sumOfLeftLeaves(self, root: TreeNode) -> int: 
  3.         """ 
  4.         Idea: Each time check current node's left node.  
  5.               If current node don't have one, skip it.  
  6.         """ 
  7.         stack = [] 
  8.         if root:  
  9.             stack.append(root) 
  10.         res = 0 
  11.          
  12.         while stack:  
  13.             # 每次都把當前節(jié)點的左節(jié)點加進去.  
  14.             cur_node = stack.pop() 
  15.             if cur_node.left and not cur_node.left.left and not cur_node.left.right:  
  16.                 res += cur_node.left.val 
  17.                  
  18.             if cur_node.left:  
  19.                 stack.append(cur_node.left) 
  20.             if cur_node.right:  
  21.                 stack.append(cur_node.right) 
  22.                  
  23.         return res 

網(wǎng)頁名稱:做了這么多題目了,會求左葉子之和么?
網(wǎng)站鏈接:http://www.dlmjj.cn/article/djiicgi.html