二叉树中最长的连续序列

时间:2017-05-26 19:33:18

标签: data-structures binary-tree binary-search-tree

我试图实现"二进制树中最长的连续序列"的逻辑。我在方法{longestConsecutivePath}中实现的逻辑对树结构的预期效果不正常。它给我的对数路径长度为5。 输出: curLength:5

    BSTNode node = new BSTNode(1);
    node.setRight(new BSTNode(2));
    node.getRight().setRight(new BSTNode(3));
    node.getRight().getRight().setRight(new BSTNode(4));
    node.getRight().getRight().getRight().setRight(new BSTNode(5));

    node.getRight().setLeft(new BSTNode(7));
    node.getRight().getLeft().setLeft(new BSTNode(8));
    node.getRight().getLeft().getLeft().setLeft(new BSTNode(9));
    node.getRight().getLeft().getLeft().getLeft().setLeft(new BSTNode(10));
    node.getRight().getLeft().getLeft().getLeft().getLeft().setLeft(new BSTNode(11));
    node.getRight().getLeft().getLeft().getLeft().getLeft().getLeft().setLeft(new BSTNode(12));

实现最长连续序列逻辑的类:

public class LongestConsecutivePath {

static BSTNode root = null;

public LongestConsecutivePath() {
    root = createBinaryTree();
    System.out.println("Before finding the longest consecutive path:");
    inorder();
}

public void inorder() {
    if (null == root) {
        return;
    }
    inorder(root);
}

private void inorder(BSTNode node) {
    if (null != node) {
        inorder(node.getLeft());
        System.out.print(node.getData() + " ");
        inorder(node.getRight());
    }
}

public BSTNode createBinaryTree() {
    BSTNode node = new BSTNode(1);
    node.setRight(new BSTNode(2));
    node.getRight().setRight(new BSTNode(3));
    node.getRight().getRight().setRight(new BSTNode(4));
    node.getRight().getRight().getRight().setRight(new BSTNode(5));

    node.getRight().setLeft(new BSTNode(7));
    node.getRight().getLeft().setLeft(new BSTNode(8));
    node.getRight().getLeft().getLeft().setLeft(new BSTNode(9));
    node.getRight().getLeft().getLeft().getLeft().setLeft(new BSTNode(10));
    node.getRight().getLeft().getLeft().getLeft().getLeft().setLeft(new BSTNode(11));
    node.getRight().getLeft().getLeft().getLeft().getLeft().getLeft().setLeft(new BSTNode(12));

    return node;
}

public int longestConsecutivePath() {
    if (null == root) {
        return 0;
    }

    return longestConsecutivePath(root, 0, root.getData() + 1);
}

public int longestConsecutivePath(BSTNode node, int curLength,
        int targetLength) {
    if (null == node) {
        return curLength;
    }

    if (node.getData() == targetLength) {
        System.out.println("\nnode data value: "+node.getData());
        curLength += 1;
        longestPath = curLength;
    } else {
        curLength = 1;
    }

    longestLeft = longestConsecutivePath(node.getLeft(), curLength,
            node.getData() + 1);
    longestRight = longestConsecutivePath(node.getRight(), curLength,
            node.getData() + 1);

    return Math.max(curLength, Math.max(longestLeft, longestRight));
}

public static void main(String[] args) {
    LongestConsecutivePath consecutivePath = new LongestConsecutivePath();
    int curLength = consecutivePath.longestConsecutivePath();
    System.out.println("\ncurLength : " + curLength);
  }
}

BSTNode.java

public class BSTNode {

BSTNode left, right;
int data;

/* Default constructor */
public BSTNode() {
    left = null;
    right = null;
    data = 0;
}

/* Constructor */
public BSTNode(int data) {
    left = null;
    right = null;
    this.data = data;
}

public BSTNode getLeft() {
    return left;
}

public void setLeft(BSTNode left) {
    this.left = left;
}

public BSTNode getRight() {
    return right;
}

public void setRight(BSTNode right) {
    this.right = right;
}

public int getData() {
    return data;
}

public void setData(int data) {
    this.data = data;
  }
}

0 个答案:

没有答案