二叉树中和为某一值的路

来源:互联网 发布:java程序员专用壁纸 编辑:程序博客网 时间:2024/06/05 11:11
题意描述:输入一棵二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。如:
        10
        /    \
      5    12
    /   \

  4    7

解题思路:借助栈,进行深度优先遍历,如果当前结点为叶子结点并且从根结点到当前结点的值的和等于给定值,则找到一条路径并打印,否则继续向叶结点遍历或者返回上一级,返回上一级时需要在当前计算的和的基础上减去当前结点的值

(C++版本)

struct BinaryTreeNode {int m_nValue;BinaryTreeNode* m_pLeft;BinaryTreeNode* m_pRight;};//创建二叉树结点BinaryTreeNode* CreateBinaryTreeNode(int value) {BinaryTreeNode* pNode = new BinaryTreeNode();pNode->m_nValue = value;pNode->m_pLeft = NULL;pNode->m_pRight = NULL;return pNode;}//连接二叉树结点void ConnectionTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight) {if (pParent != NULL) {pParent->m_pLeft = pLeft;pParent->m_pRight = pRight;}}void findPath(BinaryTreeNode* pRoot, int expectedSum, vector<int>& path, int currentSum) {currentSum += pRoot->m_nValue;path.push_back(pRoot->m_nValue);//如果是叶结点,并且路径上结点的和等于输入的值打印出这条路径bool isLeaf = (pRoot->m_pLeft == NULL) && (pRoot->m_pRight == NULL);if (isLeaf && (currentSum == expectedSum)) {cout << "A path is found: ";//vector<int>::iterator it = path.begin();//for (; it != path.end(); ++it)//cout << *it << " ";for (vector<int>::size_type i = 0; i < path.size(); i++)cout << path[i] << " ";cout << endl;}//如果不是叶结点if (pRoot->m_pLeft != NULL)findPath(pRoot->m_pLeft, expectedSum, path, currentSum);if (pRoot->m_pRight != NULL)findPath(pRoot->m_pRight, expectedSum, path, currentSum);//在返回父结点之前,在路径上删除当前结点path.pop_back();}void findPath(BinaryTreeNode* pRoot, int expectedSum) {if (pRoot == NULL)return;vector<int> path;int currentSum = 0;findPath(pRoot, expectedSum, path, currentSum);}//先序遍历void InOrder(BinaryTreeNode* pTreeRoot) {if (pTreeRoot->m_pLeft != NULL)InOrder(pTreeRoot->m_pLeft);cout << pTreeRoot->m_nValue << " ";if (pTreeRoot->m_pRight != NULL)InOrder(pTreeRoot->m_pRight);}int main(){BinaryTreeNode* node1 = CreateBinaryTreeNode(10);BinaryTreeNode* node2 = CreateBinaryTreeNode(5);BinaryTreeNode* node3 = CreateBinaryTreeNode(12);BinaryTreeNode* node4 = CreateBinaryTreeNode(4);BinaryTreeNode* node5 = CreateBinaryTreeNode(7);ConnectionTreeNodes(node1, node2, node3);ConnectionTreeNodes(node2, node4, node5);InOrder(node1);cout << endl;findPath(node1, 22);    return 0;}
(Java版本)

class BinaryTreeNode{int value;BinaryTreeNode leftNode;BinaryTreeNode rightNode;}
public class FindPath {//解题思路:借助栈,进行深度优先遍历,如果当前结点为叶子结点并且从根结点到当前结点的值的和等于给定值,则找到一条路径并打印//否则继续向叶结点遍历或者返回上一级,返回上一级时需要在当前计算的和的基础上减去当前结点的值public static void findPath(BinaryTreeNode root, int expectedSum){if(root == null)return;Stack<Integer> stack = new Stack<Integer>();int currentSum = 0;findPath(root, currentSum, stack, expectedSum);}public static void findPath(BinaryTreeNode root,int currentSum, Stack<Integer> path, int expectedSum){currentSum += root.value;path.add(root.value);//如果是叶结点,并且路径上结点的和等于输入的值则打印boolean isLeaf = (root.leftNode==null)&&(root.rightNode==null);if(isLeaf && (currentSum == expectedSum)){System.out.print("A path find: ");for(int n : path)System.out.print(n + " ");System.out.println();}//如果不是叶结点if(root.leftNode != null)findPath(root.leftNode, currentSum, path, expectedSum);if(root.rightNode != null)findPath(root.rightNode, currentSum, path, expectedSum);//在返回父结点之前把路径上的当前结点删除path.pop();}public static void main(String[] args) {BinaryTreeNode root = new BinaryTreeNode();BinaryTreeNode node1 = new BinaryTreeNode();BinaryTreeNode node2 = new BinaryTreeNode();BinaryTreeNode node3 = new BinaryTreeNode();BinaryTreeNode node4 = new BinaryTreeNode();root.leftNode = node1;root.rightNode = node2;node1.leftNode = node3;node1.rightNode = node4;root.value = 10;node1.value = 5;node2.value = 12;node3.value = 4;node4.value = 7;findPath(root, 22);}}




0 0