L3:06 - Binary Tree DFS Template

来源:互联网 发布:淘宝号查号网站 编辑:程序博客网 时间:2024/06/04 20:08

Binary Tree DFS template


/** * Copyright: NineChapter * - Algorithm Course, Mock Interview, Interview Questions * - More details on: http://www.ninechapter.com/ */Template 1: Traversepublic class Solution {    public void traverse(TreeNode root) {        if (root == null) {            return;        }        // do something with root        traverse(root.left);        // do something with root        traverse(root.right);        // do something with root    }}Tempate 2: Divide & Conquerpublic class Solution {    public ResultType traversal(TreeNode root) {        // null or leaf        if (root == null) {            // do something and return;        }                // Divide        ResultType left = traversal(root.left);        ResultType right = traversal(root.right);                // Conquer        ResultType result = Merge from left and right.        return result;    }}


0 0
原创粉丝点击