P143、面试题25:二叉树中和为某一值的路径
题目:输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。二叉树结点的定义如下:
struct BinaryTreeNode{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}
测试用例:
1)功能测试(二叉树有一条、多条符合条件的路径,二叉树中没有符合条件的路径);
2)特殊输入测试(指向二叉树根结点的指针为null指针)。
代码实现:
package com.yyq; import java.util.Iterator; import java.util.Stack; /** * Created by Administrator on 2015/9/20. */ public class PathInTree { public static void findPath_1(BinaryTreeNode pRoot, int expectedSum){ if (pRoot == null) return; Stack<Integer> path = new Stack<Integer>(); int currentSum = 0; findPath_2(pRoot, expectedSum, path, currentSum); } public static void findPath_2(BinaryTreeNode pRoot,int expectedSum,Stack<Integer> path,int currentSum){ currentSum += pRoot.getM_nValue(); path.push(pRoot.getM_nValue()); //如果是叶结点,并且路径上结点的和等于输入的值 //打印出这条路径 boolean isLeaf = pRoot.getM_pLeft() == null && pRoot.getM_pRight() == null; if (currentSum == expectedSum && isLeaf){ System.out.println("A path is found:"); Iterator<Integer> it = path.iterator(); while(it.hasNext()){ System.out.print(it.next() + "\t"); } System.out.println(); } //如果不是叶结点,则遍历它的子节点 if (pRoot.getM_pLeft()!= null){ findPath_2(pRoot.getM_pLeft(), expectedSum, path, currentSum); } if (pRoot.getM_pRight()!=null){ findPath_2(pRoot.getM_pRight(), expectedSum, path, currentSum); } // 在返回到父结点之前,在路径上删除当前结点, // 并在currentSum中减去当前结点的值 currentSum -= pRoot.getM_nValue(); path.pop(); } // ====================测试代码==================== public static void Test(String testName, BinaryTreeNode pRoot, int expectedSum) { if(testName != null) System.out.println(testName+" begins:"); findPath_1(pRoot, expectedSum); System.out.println(); } // 10 // / \ // 5 12 // /\ // 4 7 // 有两条路径上的结点和为22 public static void Test1() { BinaryTreeNode pNode10 = new BinaryTreeNode(10); BinaryTreeNode pNode5 = new BinaryTreeNode(5); BinaryTreeNode pNode12 = new BinaryTreeNode(12); BinaryTreeNode pNode4 = new BinaryTreeNode(4); BinaryTreeNode pNode7 = new BinaryTreeNode(7); pNode10.connectTreeNodes(pNode5, pNode12); pNode5.connectTreeNodes(pNode4, pNode7); System.out.println("Two paths should be found in Test1."); Test("Test1", pNode10, 22); pNode10 = null; } // 10 // / \ // 5 12 // /\ // 4 7 // 没有路径上的结点和为15 public static void Test2() { BinaryTreeNode pNode10 = new BinaryTreeNode(10); BinaryTreeNode pNode5 = new BinaryTreeNode(5); BinaryTreeNode pNode12 = new BinaryTreeNode(12); BinaryTreeNode pNode4 = new BinaryTreeNode(4); BinaryTreeNode pNode7 = new BinaryTreeNode(7); pNode10.connectTreeNodes(pNode5, pNode12); pNode5.connectTreeNodes(pNode4, pNode7); System.out.println("No paths should be found in Test2."); Test("Test2", pNode10, 15); pNode10 =null; } // 5 // / // 4 // / // 3 // / // 2 // / // 1 // 有一条路径上面的结点和为15 public static void Test3() { BinaryTreeNode pNode5 = new BinaryTreeNode(5); BinaryTreeNode pNode4 = new BinaryTreeNode(4); BinaryTreeNode pNode3 = new BinaryTreeNode(3); BinaryTreeNode pNode2 = new BinaryTreeNode(2); BinaryTreeNode pNode1 = new BinaryTreeNode(1); pNode5.connectTreeNodes(pNode4, null); pNode4.connectTreeNodes(pNode3, null); pNode3.connectTreeNodes(pNode2, null); pNode2.connectTreeNodes(pNode1, null); System.out.println("One path should be found in Test3."); Test("Test3", pNode5, 15); pNode5 = null; } // 1 // \ // 2 // \ // 3 // \ // 4 // \ // 5 // 没有路径上面的结点和为16 public static void Test4() { BinaryTreeNode pNode1 = new BinaryTreeNode(1); BinaryTreeNode pNode2 = new BinaryTreeNode(2); BinaryTreeNode pNode3 = new BinaryTreeNode(3); BinaryTreeNode pNode4 = new BinaryTreeNode(4); BinaryTreeNode pNode5 = new BinaryTreeNode(5); pNode1.connectTreeNodes(null, pNode2); pNode2.connectTreeNodes(null, pNode3); pNode3.connectTreeNodes(null, pNode4); pNode4.connectTreeNodes(null, pNode5); System.out.println("No paths should be found in Test4."); Test("Test4", pNode1, 16); pNode1 = null; } // 树中只有1个结点 public static void Test5() { BinaryTreeNode pNode1 = new BinaryTreeNode(1); System.out.println("One path should be found in Test5."); Test("Test5", pNode1, 1); pNode1 = null; } // 树中没有结点 public static void Test6() { System.out.println("No paths should be found in Test6."); Test("Test6", null, 0); } public static void main(String[] args) { Test1(); Test2(); Test3(); Test4(); Test5(); Test6(); } }
结果输出:
Two paths should be found in Test1.
Test1 begins:
A path is found:
10 5 7
A path is found:
10 12
No paths should be found in Test2.
Test2 begins:
One path should be found in Test3.
Test3 begins:
A path is found:
5 4 3 2 1
No paths should be found in Test4.
Test4 begins:
One path should be found in Test5.
Test5 begins:
A path is found:
1
No paths should be found in Test6.
Test6 begins: