/* Template 1: Traverse*/publicclassSolution {publicvoidtraverse(TreeNode root) {if (root ==null) {return; }// do something with roottraverse(root.left);// do something with roottraverse(root.right); }}
Template 2: Divide and Conquer
/* Template 2: Divide and Conquer*/publicclassSolution {publicResultTypetraverse(TreeNode root) {if (root ==null) {//do something and return;//return condition }//divideResultType left =traverse(root.left);ResultType right =traverse(root.right);//conquerResultType result =Merge from left and right;return result; }}