二叉树的前序便利
public class TreeNode{ //左子节点 private TreeNode left = null; //又子节点 private TreeNode right = null; //数据 private int data = 0; //前序遍历访问 private static String headFirstVisit(TreeNode root){ if(root == null){ return ""; } if(root.left == null && root.right == null){ return root.data + ","; } StringBuilder path = new StringBuilder(root.data + ","); if(root.left != null){ path.append(headFirstVisit(root.left)); } if(root.right != null){ path.append(headFirstVisit(root.right)); } return path; }}
?