码迷,mamicode.com
首页 > 其他好文 > 详细

Binary Search Tree DFS Template

时间:2015-10-26 01:56:04      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

Two methods:

1. Traverse

2. Divide & Conquer

 1 // Traverse: usually do not have return value
 2 public class Solution {
 3     public void traverse(TreeNode root) {
 4         if (root == null)
 5             return;
 6         traverse(root.left);
 7         traverse(root.right);
 8     }
 9 }
10 
11 // Divide & Conquer: usually have return value
12 public class Solution {
13     public ResultType traversal(TreeNode root) {
14         if (root == null) {
15             // do something and return
16         }
17 
18         // Divide
19         ResultType left = traversal(root.left);
20         ResultType right = traversal(root.right);
21 
22         // Conquer
23         ResultType merge = Merge from left to right.
24         return result;
25     }
26 }

 

Binary Search Tree DFS Template

标签:

原文地址:http://www.cnblogs.com/ireneyanglan/p/4910118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!